简体   繁体   中英

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

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);

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM