Categories
JavaScript Answers

How to access EJS variable in JavaScript logic?

Spread the love

To access EJS variable in JavaScript logic, we can inject the variable directly into the EJS template.

For instance, we write

<% if (gameState) { %>
<script>
  let clientGameState = <%= gameState %>
</script>
<% } %>

to get the gameState variable with <%= gameState %> and assign that to the clientGameState variable in the script tag.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *