1. Character conversion
varS1 = "01"; varS2 = "1.1"; varS3 = "Z";//the letter ' Z ' cannot be converted to a number, so or return nanvarb =false; varF = 1.1; varo ={valueOf:function() { return-1; } }; S1=-S1;//value becomes numeric-1s2 =-s2;//value becomes numeric-1.1s3 =-S3;//value becomes NaNb =-C;//value becomes numeric 0f =-F;//Change to-1.1o =-O;//1 The ValueOf () method of the first execution object returns-1,--1 is 1
o = valueof () value of +o;//-1 O is -1,+-1 or-1
2. Special character arithmetic
varRESULT1 = 5-true;//4 because true is converted to 1 varRESULT2 = NaN-1;//nan Nan is not a number, and any number doing any operation is Nan varRESULT3 = 5-3;//2 varRESULT4 = 5-"";//5 because "" is converted to 0 varRESULT5 = 5-"2";//3 because "2" is converted to 2 varRESULT6 = 5-NULL;//5 because null is converted to 0
3. Variable-to-string operation
var value1 = 10; var value2 = true ; var value3 = null ; var Value4;//value4 no assignment is underfined Converting to a string is ' underfined ' alert (String (value1)); // " alert (String (value2)); // alert (String (VALUE3)); // alert (String (VALUE4)); // "undefined"
4. Conversion of numbers into the system
var num = ten; Alert (num.tostring ()); // "10" Default decimal Alert (num.tostring (2)); // "1010" Binary Alert (num.tostring (8)); // "12" octal Alert (Num.tostring (ten)); // "10" decimal Alert (num.tostring ()); // "a" hex
5. String comparison operation
varRESULT1 = 5 > 3;//true varRESULT2 = 5 < 3;//false varRESULT3 = "Brick" < "Alphabet";//true string comparisons are compared in alphabetical order with lowercase letters behind uppercase letters varRESULT4 = "Brick". toLowerCase () < "Alphabet". toLowerCase ();//false alphabetical order comparison varRESULT5 = "All" < "3";//true character 2 is less than character 3 varRESULT6 = "3" <;//false when ' 23 ' is converted to 23. varRESULT7 = "a" < 3;//false because "a" becomes NaN character ' a ' cannot be converted to a number varRESULT8 = NaN < 3;//false NaN and any number comparison cannot be converted to numbers, so it is always false varResult9 = NaN >= 3;//false
6. Conversion of character into the system
var num1 = parseint ("AF", +); // 175 Output Decimal data according to the 16 binary 10*16+15*1 var num2 = parseint ("AF"); // Nan does not have a specified binary, by default the 10 binary conversion, because AF is not in the decimal range, returns NaN alert (NUM1); alert (num2);
7, the use of parseint
varNUM1 = parseint ("1234blue");//1234 varnum2 = parseint ("");//The NaN character ' cannot be converted to a number varnum3 = parseint ("0xA");//10-hexadecimal 16 binary a varNUM4 = parseint (22.5);// A varNUM5 = parseint ("70");//70-decimal varNUM6 = parseint ("0xf");//15 16 binary for
8. Use of Number objects
var num1 = number ("Hello world!"); // NaN var num2 = number (""); // 0 empty strings can be converted to 0 this parseint () is not the same var num3 = number ("000011"); // One var num4 = number (true); // 1
9. Nan Usage
Alert (nan = = nan); // false Alert (IsNaN (NaN)); // true Alert (IsNaN (ten)); // false? is a number Alert (IsNaN ("ten")); // false? Can is converted to number Alert (IsNaN ("Blue")); // true? Cannot is converted to a number Alert (IsNaN (true)); // false? can converted to number 1
10, the system maximum number
var result = Number.MAX_VALUE + 1; Alert (isfinite (result)); // false
11, Infinity Infinity
Alert (5 * 6); // 30 alert (5 * NaN); // nan alert (Infinity * 0); // nan alert (Infinity * 2); // infinity alert ("5" * 5); // 25 alert (true * 10); // 10 alert (false * 10); // 0
Alert (26 5); 1
Alert (Infinity% 3); NaN
Alert (3 0); NaN
Alert (5 Infinity); 5
Alert (0 10); 0
Alert (true% 25); 1
Alert (3 false); NaN
12. For In loop
for (var in window) { document.write (propname); document.write ("<br/>"); }
13. Special character comparison
AlertNULL= = undefined);//trueAlertNULL= = = undefined);//falseAlert ("Nan" = = Nan);//falseAlert ("nan" = = = Nan);//falseAlert (nan = = Nan);//falseAlert (nan = = = Nan);//falseAlert (nan! = nan);//trueAlert (Nan!== nan);//trueAlert (false= = 0);//trueAlertfalse= = = 0);//falseAlerttrue= = 1);//trueAlerttrue= = = 1);//falseAlert (NULL= = 0);//falsealert (undefined = = 0);//falseAlert (5 = = "5");//trueAlert (5 = = = "5");//false
14. A Boolean object
var message = "Hello world!" ; var messageasboolean = Boolean (message); // true
15. For loop use with label
Breakstatements, andContinuestatements can be used in conjunction with a tagged statement to return a specific location in the code. Typically, this is done when there is a loop inside the loop, for example:varnum = 0; //return to a specific locationOutermost: for(vari=0; I < 10; i++) { for(varj=0; J < 10; J + +) { if(i = = 5 && J = = 5) { Breakoutermost; } num++; }} alert (num); //55
In the example above, the label outermost represents the first for statement. Normally, each for statement executes 10 blocks of code, which means that num++ will normally be executed 100 times, and Num should be equal to 100 when execution is complete. The break statement here has a parameter, which is the label of the statement to jump to after stopping the loop. This way, the break statement can not only jump out of the internal for statement (that is, the statement using the variable J), but also jump out of the external for statement (that is, the statement using the variable i). Therefore, the last value of NUM is 55 because the loop terminates when both I and J values are equal to 5 o'clock.
You can use the Continue statement in the same way:
var iNum = 0; outermost: for (var i=0; i<10; i++) { for ( var j=0; j<10; J + +) { if (i = = 5 && j = 5) { continue outermost; } iNum+ +; }} alert (iNum); // Output ""
In the above example, the Continue statement forces the loop to continue, not just the inner loop, but also the outer loop. This occurs when J equals 5 o'clock, which means that the internal loop will be reduced by 5 iterations, resulting in a value of INum of 95.
Tip: It can be seen that tagged statements used in conjunction with break and continue are very powerful, but overuse of them will cause trouble with debugging code. Be sure to use a label that is descriptive and do not nest too many layers of loops.
JavaScript Learning Summary (i)