How to pass Variable to inline JavaScript using EJS Template Engine? Last Updated : 24 Jul, 2024 Comments Improve Suggest changes Like Article Like Report In the EJS templating engine, we can pass variables to inline JS within our HTML templates. We can use the '<%= variable %>' syntax, where we can embed server-side variables directly into the client-side scripts. In this article, we will explore the detailed process to pass a variable to inline javascript using the EJS templating engine in terms of practical examples with output.What is EJS templating Engine?EJS (Embedded JavaScript) is a popular templating engine for Node.js that allows to generate HTML markup with JavaScript. One common requirement in web development is passing variables from server-side code to client-side JavaScript for dynamic content rendering and interaction.Syntax for passing variable:<script> var myVariable = '<%= serverSideVariable %>'; // use myVariable in JS code</script>Steps to pass a variable to inline javascript using EJS templating engineStep 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 ejsUpdated Dependencies: The updated dependencies in package.json file will look like "dependencies": { "express": "^4.18.2", "ejs": "^3.1.9",}Step 4: Now create the below Project Structure in your project which includes app.js and views/index.ejs files.Step 5: Now, create the required files and add the following codes. HTML <!-- views/index.ejs --> <!DOCTYPE html> <head> <title>Example</title> </head> <body> <h1>Welcome to GeeksforGeeks!</h1> <script> // inline JavaScript var message = '<%= data.message %>'; alert(message); </script> </body> </html> JavaScript //app.js const express = require('express'); const app = express(); const port = 3000; app.set('view engine', 'ejs'); app.set('views', __dirname + '/views'); app.get('/', (req, res) => { const data = { message: 'Hello from GeeksforGeeks!' }; res.render('index', { data }); }); app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); To run the application, we need to start the server by using the below command.node app.jsOutput: Comment More infoAdvertise with us Next Article How to pass Variable to inline JavaScript using EJS Template Engine? G gauravgandal Follow Improve Article Tags : Web Technologies Node.js EJS-Templating Language View Engine Similar Reads How to print a variable directly using EJS template engine? EJS (Embedded JavaScript) is a templating engine for NodeJS that enables dynamic content generation in web applications. To print a variable directly in an EJS template, you can use the <%= variable %> syntax. This syntax allows you to embed and display the value of a variable directly within 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 Pass/Access Node.js Variable to an HTML file/template ? When building web applications with Node.js, itâs often necessary to pass server-side variables to the client-side for rendering dynamic content. This can be achieved using various techniques and templating engines. This article will guide you through different methods to pass and access Node.js var 2 min read How to Install & Use EJS Template Engine ? EJS (Embedded JavaScript) is mainly a templating language for JS which allows dynamic content generation in web-based applications. This EJS Template allows us to embed the JS code directly in the HTML markup, which enables the integration of data and logic into the presentation layer. We can use EJ 3 min read How to Use Embedded JavaScript (EJS) as a Template Engine in Express JS ? Embedded JavaScript (EJS) is a simple templating language that helps us to generate HTML markup with plain JavaScript. It's commonly used with Node.js to create dynamic web pages. It also helps to incorporate JavaScript into HTML pages. Approach to use EJS as Template Engine in Express JS:Install EJ 2 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 Access EJS Variable in Javascript Logic ? 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 3 min read How to use array of objects in EJS Template Engine ? Arrays of objects are collections of JavaScript objects stored within an array. Each object within the array can contain multiple key-value pairs, making them versatile for organizing various types of data. For example, you might have an array of objects representing users, products, or posts in a s 3 min read How to Access Variables from Another File using JavaScript? Accessing variables from another file in JavaScript involves exporting the variables from one file and importing them into another. In ES6 modules, use export to share variables and import to access them. In CommonJS (Node.js), use module.exports and require() for the same purpose.Below are the appr 3 min read How to invoke a function without parenthesis using JavaScript ? Method 1: Using the new operator. The new operator is used to create an instance of an object which has a constructor function. This constructor function can be used to write our own function and then be invoked by the new operator. The parenthesis is optional when using this operator, hence the fun 2 min read Like