Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple variables declaration in ejs

I am trying to declare and assign a default value to multiple variables. But the value is only getting assigned to last variable

<% var scale_text,scale_image = 'free_transform'; %>

This print empty:

<%- scale_text %>

This prints free_transform

<%- scale_image %>

What am i missing?

like image 745
Khawer Zeshan Avatar asked May 20 '15 15:05

Khawer Zeshan


People also ask

Can you declare variables in EJS?

A variable declaration starts with the keyword var followed by the variable name, e.g. : var myVariable; The variable declaration ends with a semi-colon. The names of the variables in EJS-templates are case-sensitive: myVariable and myvariable are different variables.

How do you declare multiple variables?

If your variables are the same type, you can define multiple variables in one declaration statement. For example: int age, reach; In this example, two variables called age and reach would be defined as integers.

How do you add variables in EJS?

Open your text editor and type the following code, save as app. js. var express = require('express'); var ejs = require('ejs'); var app = express(); app. set('view engine', 'ejs'); app.

How do you declare 3 variables in JavaScript?

Basically we can declare variables in three different ways by using var, let and const keyword. Each keyword is used in some specific conditions.


2 Answers

Separate the variables with = to set them to the same default value.

<% var scale_text = scale_image = 'free_transform'; %>

Update: Although, as @Meeker notes in their answer, this is probably better:

<% var scale_text, scale_image; %>
<% scale_text = scale_image = 'free_transform'; %>
like image 163
Trott Avatar answered Sep 28 '22 15:09

Trott


What your writing will declare scale_text as an empty variable.

To work the way you want it to you need to do the following

<% var scale_text = scale_image = 'free_transform'; %>

However this is probably preferable

<% var scale_text, scale_image; %> <% scale_text = scale_image = 'free_transform'; %>

like image 35
Meeker Avatar answered Sep 28 '22 14:09

Meeker