How to Access EJS Variable in Javascript Logic ? Last Updated : 08 Apr, 2024 Comments Improve Suggest changes Like Article Like Report EJS stands for Embedded JavaScript. It is a templating language used to generate dynamic HTML pages with data from the server by embedding JavaScript code. Features of EJSDynamic Content: Based on data from servers or other sources, we can generate a dynamic HTML template.Partial Templates: Partials are reusable chunks of HTML markup that can be included in multiple templates. We can create header.js and footer.js partials that can be added by all the templates (home.js, contact. js). This ensures code reusability.Control Structures: We can use loops, conditions, and iterations over an array/object to build complex templates.Embedded JavaScript : Using special tags ( <% %>, <%= %>, and <%- %> ) , we can embed JavaScript in HTML markup. EJS replaces <%= %> tags, with the actual value of the variable when rendering the page.Steps to Access EJS variable in Javascript logicStep 1: Firstly, we will make the folder named root by using the below command in the VScode Terminal. After creation use the cd command to navigate to the newly created folder. mkdir rootcd rootStep 2: Once the folder is been created, we will initialize NPM using the below command, this will give us the package.json file. npm init -yStep 3: Once the project is been initialized, we need to install Express and EJS dependencies in our project by using the below installation command of NPM. npm i express ejsProject Structure The updated dependencies in package.json file will look like: "dependencies": { "express": "^4.18.2", "ejs": "^3.1.9",}Explanation:Here, we have used special tags like <%= %> and <% %> to access EJS variables in JS. We are using <% %> for adding control strcture( like here we used if-else within <% %> within HTML tags. Here, we are accessing the variable role within if condition and since <% %> are already added , we can directly access role variable. To print value of name , we are using <%= %> tag (<%=name%>) .We are accessing EJS varible within script tag by using <%= %> tag ( <%=role%> ), saving it in role variable and then using its value. <%=name%> and <%=role%> will be replaced with value of name and role variable respectively when the template index.ejs is rendered. Example: Below is an example of Accessing EJS variable in Javascript logic. HTML <!DOCTYPE html> <html lang="en"> <head> <title>Access EJS Variable in JS</title> </head> <body> <h1>Hello, <%if(role=='Admin'){%> <%= name %> <%}else{%> Guest <%}%> ! </h1> <script> var role = '<%= role %>'; if (role === 'Admin') { alert('Welcome to GFG, Admin!'); } else { alert('Welcome to GFG, Guest!'); } </script> </body> </html> JavaScript // app.js const express = require('express'); const path = require('path'); const app = express(); const PORT = process.env.PORT || 4000; // Set the view engine to EJS app.set('view engine', 'ejs'); // Define a route to render the HTML file app.get('/', (req, res) => { res.render('index', { role: 'Admin', name: 'GFG' }); }); // Start the server app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); }); Output: Comment More infoAdvertise with us Next Article How to Access EJS Variable in Javascript Logic ? K kumariashish96 Follow Improve Article Tags : JavaScript Web Technologies EJS-Templating Language Similar Reads How to create a private variable in JavaScript ? In this article, we will try to understand how we could create private variables in JavaScript. Let us first understand what are the ways through which we may declare the variables generally in JavaScript. Syntax: By using the following syntaxes we could declare our variables in JavaScript. var vari 3 min read How to Declare Multiple Variables in JavaScript? JavaScript variables are used as container to store values, and they can be of any data type. You can declare variables using the var, let, or const keywords. JavaScript provides different ways to declare multiple variables either individually or in a single line for efficiency and readability.Decla 2 min read How to Pass variables to JavaScript in Express JS ? Express is a lightweight framework that sits on top of Node.jsâs web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your applicationâs functionality with middleware and routing. When working with Express.js you may encounter scenarios where you 2 min read How to declare Global Variables in JavaScript ? Global Variables Global variables in JavaScript are variables declared outside of any function. These variables are accessible from anywhere within the script, including inside functions. Global variables are declared at the start of the block(top of the program)Var keyword is used to declare variab 2 min read How to access the Value of a Promise in JavaScript In this article, we will see how to access the value of Promise in JavaScript. The Promise is a feature of ES6 introduced in 2015. The concept of Promises is generally used when we want to work asynchronously. The value of a Promise can be accessed in JavaScript using the following methods. Table of 2 min read JavaScript - How to Use a Variable in Regular Expression? To dynamically create a regular expression (RegExp) in JavaScript using variables, you can use the RegExp constructor. Here are the various ways to use a variable in Regular Expression.1. Using the RegExp Constructor with a VariableIn JavaScript, regular expressions can be created dynamically using 3 min read How to Use Dynamic Variable Names in JavaScript? Dynamic variable names are variable names that are not predefined but are generated dynamically during the execution of a program. This means the name of a variable can be determined at runtime, rather than being explicitly written in the code.Here are different ways to use dynamic variables in Java 2 min read How to use JavaScript Variables in Ruby? JavaScript and Ruby are two powerful programming languages used extensively in web development. While each language has its own set of features and capabilities, there are times when developers may need to integrate functionalities from one language into the other. One common scenario is using JavaS 4 min read How to use Ejs in JavaScript ? EJS or Embedded Javascript Templating is a templating engine used by Node.js. The template engine helps to create an HTML template with minimal code. Also, it can inject data into the HTML template on the client side and produce the final HTML. Installation StepsInstall the module using the followin 3 min read Global and Local variables in JavaScript In JavaScript, understanding the difference between global and local variables is important for writing clean, maintainable, and error-free code. Variables can be declared with different scopes, affecting where and how they can be accessed. Global VariablesGlobal variables in JavaScript are those de 4 min read Like