Edit : this half believes that you are using server- side EJS
1) You can pass the value of the ejs variable to a Javascript variable
<% var test = 101; %> // variable created by ejs <script> var getTest = <%= test %>; //var test is now assigned to getTest which will only work on browsers console.log(getTest); // successfully prints 101 on browser </script>
just create an ejs variable and assign a value inside the script tag var getTest script
Example: var getTest = <%= test %>;
2) You cannot pass the value of javascript variable to ejs variable
Yes, you cannot: if it is on the server.
Why:
The EJS template will be displayed on the server until Javscript is launched (it will be launched in the browser), so the server will not return to the server and ask for some previous changes on the page that has already been sent to the browser page.
Edit : this half believes that you are using EJS on the
Client side
3) if EJS is on the client side and passes the EJS variable to javascript
The above answer will still work, but you will need to load the script into the EJS template , not the script loaded before the created template (in this case, of course, it will not be valid javascript).
4) if EJS is on the client side and passes the javascript variable to EJS
I'm sorry that I myself have not tried this case, but I really wait if someone answers this case
source share