As has already been said, Twig runs in PHP on the server and the JavaScript variable is on the client. To use it in a server-side template, you'd need to get the variable value back to the server using an AJAX request and then use that response to replace the content in the browser.
A potential way to do what you're asking is to use a JavaScript implementation of Twig like twig.js. This would allow your Twig templates to use JavaScript variables without having to go back to the server.
<script src="https://cdnjs.cloudflare.com/ajax/libs/twig.js/0.8.9/twig.min.js">
</script>
<script type="text/javascript" charset="utf-8">
var test = "1";
var template = twig({
data: 'The value of test is {{ test }}.'
});
document.getElementById('some-element').innerHTML = template.render({ test: test });
</script>
I have a feeling that this is overkill for your purposes. It really depends what your use case is and what you're trying to achieve.