Paul Baiju
Paul Baiju

Reputation: 419

How to find the sum of variables in javascript?

var pagenumber = localStorage["pageno"];
var sum = pagenumber + i;
display(sum);

In this case i am getting the output sum = 11 (when input pagenumber = 1 and i = 1) , But i require the output sum = 2

Upvotes: 0

Views: 304

Answers (2)

IamCavic
IamCavic

Reputation: 368

You can use parseInt to convert your strings to integers in JavaScript:

var sum = parseInt(localStorage["pageno"]) + parseInt(i);
display(sum);

Upvotes: 0

Kyle
Kyle

Reputation: 3308

Need to parse the "1" in localStorage to an integer using the built in parseInt funtion:

var pagenumber = localStorage["pageno"];
var sum = Number.parseInt(pagenumber) + i;
display(sum);

Upvotes: 2

Related Questions