I am sure there are several viable solutions, but I would use data attributes.
So basically you render the variables via your templates as HTML, and then access them in your JavaScript.
Example from the linked article
<article
id="electric-cars"
data-columns="3"
data-index-number="12314"
data-parent="cars">
...
</article>
And then you'd access the element and its attributes via JavaScript.
As an alternative, you could generate the JavaScript code from your backend.
As a third way, you could call your backend via http from your JavaScript code.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…