pritam_parker
pritam_parker

Reputation: 19

How can I add two numbers using ejs(Embedded JavaScript)?

I want to add 2 numbers which is coming from the back end. I am trying to add using <%=num1%> + <%=num2%> but its printing num1num2 instead of num1+num2. What I am doing wrong and what is alternative solution?

Upvotes: 0

Views: 2607

Answers (2)

AJS
AJS

Reputation: 993

pass numbers to template from your controller.

res.render('index', { result: {n1:1,n2:2} });

and then in ejs template just like javascript

<%= n1+ n2 %>

Upvotes: 0

Dennington-bear
Dennington-bear

Reputation: 1782

This is done by

<%= num1 + num2 %>

What you are doing is only displaying the elements because when you have '<%=' this means to display this item. Another way to do this can be.

// Add the numbers
<% var addedNumber = num1 + num2; %>
//Then display the added numbers
<%= addedNumber %>

Upvotes: 2

Related Questions