How do I print a twig variable inside a JavaScript in a twig file I have a twig file of a custom module. It has a &lt;script&gt;&lt;/script&gt; section and I need to print a twig variable inside that section. How do I do this?

{{ set twigVariable = '123456' }}

&lt;script&gt;
    function myFunc() {
        var myJsVar = {{ twigVariable }};
    }
&lt;/script&gt;


The variable does not print when I write like above.
I'll spare to tell that it's a bad practice to add custom script blocks to templates, and just point out the error(s):

(1) To set the variable content, you have to wrap set into a {% %} block, not {{ }}.

(2) You most probably want the value as JavaScript string, so don't forget to wrap it in " ".

This sums up to

{% set twigVariable = '123456' %}

&lt;script&gt;
  function myFunc() {
    var myJsVar = "{{ twigVariable }}";
  }
&lt;/script&gt;


Which should be working just fine.