Today, I was hit by the String and numeric + operations in JavaScript.
From url
Asp? Ipageno = 12 "> http://www.test.com/test.asp? Ipageno = 12
IPageNo = 12 is obtained from iPageNo, which is to jump to the iPageNo + 1 page, that is, 13 pages, but the jump to the 121 page, that is, the result of iPageNo + 1 is 121.
Why?
The following test code is written:
View plaincopy to clipboardprint?
<Mce: script language = "javascript"> <! --
Var iPageNo = "12 ";
Document. write ("var iPageNo =" 12 "; <BR> ");
Var a1 = 1 + iPageNo;
Var a2 = iPageNo + 1;
Var b1 = 1-iPageNo;
Var b2 = iPageNo-1;
Document. write ("typeof (1 + iPageNo) =" + typeof (a1) + ", 1 + iPageNo =" + a1 );
Document. write ("<BR> ");
Document. write ("typeof (iPageNo + 1) =" + typeof (a2) + ", iPageNo + 1 =" + a2 );
Document. write ("<BR> ");
Document. write ("typeof (1-iPageNo) =" + typeof (b1) + ", 1-iPageNo =" + b1 );
Document. write ("<BR> ");
Document. write ("typeof (iPageNo-1) =" + typeof (b2) + ", iPageNo-1 =" + b2 );
Document. write ("<BR> ");
IPageNo = 12;
Document. write ("iPageNo = 12; <BR> ");
Var a1 = 1 + iPageNo;
Var a2 = iPageNo + 1;
Var b1 = 1-iPageNo;
Var b2 = iPageNo-1;
Document. write ("typeof (1 + iPageNo) =" + typeof (a1) + ", 1 + iPageNo =" + a1 );
Document. write ("<BR> ");
Document. write ("typeof (iPageNo + 1) =" + typeof (a2) + ", iPageNo + 1 =" + a2 );
Document. write ("<BR> ");
Document. write ("typeof (1-iPageNo) =" + typeof (b1) + ", 1-iPageNo =" + b1 );
Document. write ("<BR> ");
Document. write ("typeof (iPageNo-1) =" + typeof (b2) + ", iPageNo-1 =" + b2 );
// --> </Mce: script>
<Mce: script language = "javascript"> <! --
Var iPageNo = "12 ";
Document. write ("var iPageNo =" 12 "; <BR> ");
Var a1 = 1 + iPageNo;
Var a2 = iPageNo + 1;
Var b1 = 1-iPageNo;
Var b2 = iPageNo-1;
Document. write ("typeof (1 + iPageNo) =" + typeof (a1) + ", 1 + iPageNo =" + a1 );
Document. write ("<BR> ");
Document. write ("typeof (iPageNo + 1) =" + typeof (a2) + ", iPageNo + 1 =" + a2 );
Document. write ("<BR> ");
Document. write ("typeof (1-iPageNo) =" + typeof (b1) + ", 1-iPageNo =" + b1 );
Document. write ("<BR> ");
Document. write ("typeof (iPageNo-1) =" + typeof (b2) + ", iPageNo-1 =" + b2 );
Document. write ("<BR> ");
IPageNo = 12;
Document. write ("iPageNo = 12; <BR> ");
Var a1 = 1 + iPageNo;
Var a2 = iPageNo + 1;
Var b1 = 1-iPageNo;
Var b2 = iPageNo-1;
Document. write ("typeof (1 + iPageNo) =" + typeof (a1) + ", 1 + iPageNo =" + a1 );
Document. write ("<BR> ");
Document. write ("typeof (iPageNo + 1) =" + typeof (a2) + ", iPageNo + 1 =" + a2 );
Document. write ("<BR> ");
Document. write ("typeof (1-iPageNo) =" + typeof (b1) + ", 1-iPageNo =" + b1 );
Document. write ("<BR> ");
Document. write ("typeof (iPageNo-1) =" + typeof (b2) + ", iPageNo-1 =" + b2 );
// --> </Mce: script>
The running result is:
Var iPageNo = "12 ";
Typeof (1 + iPageNo) = string, 1 + iPageNo = 112
Typeof (iPageNo + 1) = string, iPageNo + 1 = 121
Typeof (1-iPageNo) = number, 1-iPageNo =-11
Typeof (iPageNo-1) = number, iPageNo-1 = 11
IPageNo = 12;
Typeof (1 + iPageNo) = string, 1 + iPageNo = 112
Typeof (iPageNo + 1) = string, iPageNo + 1 = 121
Typeof (1-iPageNo) = number, 1-iPageNo =-11
Typeof (iPageNo-1) = number, iPageNo-1 = 11
Visible:
When the String type and the number type are combined, the number type is automatically converted to the String type, and the result is also a String type.
When the String type and the numeric type perform-operation, the String type is automatically converted to the numeric type, and the result is also a numeric type.
Because the iPageNo value obtained from the url is of the String type
IPageNo + 1 = "12" + 1 = "12" + "1" = "121"
Therefore, to obtain numeric data from a Url, use Number () to forcibly convert the type.