简体   繁体   English

如何在javascript中找到变量的总和?

[英]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 在这种情况下,我得到的输出总和= 11(当输入的页数= 1且i = 1时),但是我要求输出的总和= 2

Need to parse the "1" in localStorage to an integer using the built in parseInt funtion: 需要使用内置的parseInt函数将localStorage中的“ 1”解析为整数:

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

You can use parseInt to convert your strings to integers in JavaScript: 您可以使用parseInt在JavaScript中将字符串转换为整数:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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