How can I use javascript code in an external file with twig elements?

I have a lot of javascript code one my twig template home.html.twig, for example: Because the source code of my main page is now full of javascript I try to move it into an external file, to have a more clean appearance: But it does not work because I have so much twig variables, and they are not recognized anymore. For every twig variable I get an error message, like this for example: Answer First of all (as the comments already said) it is not a good idea to mix JavaScript with Twig. Makes your code hard to maintain, hard

Pass javascript variable to twig

I just want to pass a javascript variable to a twig path. Now i’m using this, but it doesn’t work.