7👍
✅
You can try this solution
<script>
{% autoescape off %}
var my_variable = "{{ variable|escapejs }}"
{% endautoescape %}
</script>
Source:stackexchange.com