1. Non-numeric type to numeric value
useNumber()
When converting:
- undefined will be converted to NaN
- If the string starts with 0, the browser ignores the leading 0 and will not convert it in octal.
- If the string starts with 0x, the browser will convert it into decimal to return in hexadecimal
- If the string has characters, it will be converted to NaN except (+,-,.). In hexadecimal, the string contains any non-numeric characters and returns NaN
- If it is an object conversion, the object first uses valueof() and then converts according to the rules. If there is no valueOf method, the toString method is called and then converted.
useparseInt()
When converting:
- parseInt ignores leading spaces until the first non-number character begins parsing, and returns NaN if it is a non-number or a positive sign. If it is a numeric, it will be parsed until the first non-number. Note: In parseInt, the decimal point is not a valid numeric character
- parseInt can recognize decimal, octal and hexadecimal, but when parsing octal, ECMAScript 3 and ECMAScript 5 have differences. ECMAScript 3 will convert 070 to 56, but ECMAScript 5 will convert 70.
- Use the second parameter of parseInt
var num1 = parseInt("10",2); //2 Analysis by binaryvar num2 = parseInt("10",8); //8 Perform octalvar num3 = parseInt("10",10); //10 Decimal analysisvar num4 = parseInt("10",16); //16 Perform in hexadecimal
useparseFloat()
When converting:
The first difference between parseFloat and parseInt is that it encounters an invalid floating-point numeric character when parsing the string, which is much more than parseInt.
Returns 0 when parsing hexadecimal values
var num = parseFloat("0xA"); //0 var num = parseInt("0xA"); //10
The parseFloat function does not have a second parameter that specifies the cardinality, so only the decimal values are parsed.
If the string is an integer, it returns an integer instead of a floating point numbervar num = parseFloat("2.125e7"); //31250000
2. Use toString() to output numerical values in different digits
This bar applies to integers, we can use toString() to return integers in any binary format.
var num = 10; alert(()); //"10" alert((9)); //"11" alert((16)); //"a"
3. Pay attention to NaN and Infinity when bit operators
When using bit operators for NaN and Infinity, both values are treated as 0. If a bit operator is applied to a non-numeric value, the value is converted to a numeric value using the Number() function.
Another thing to note is that the unsigned right shift of negative numbers, and the unsigned right shift is to fill the empty space with 0, unlike the signed right shift with sign bits, so the unsigned right shift and signed right shift of positive numbers are the same, but the negative number is different. The unsigned right shift operation will treat the negative binary code as the positive binary code, and the negative number is represented in the complement form, which will cause the result of the unsigned right shift to be very different.
var oldValue = -64; var newValue = oldValue >>> 5
4. Special numerical operations
For numerical operations, if there is an operand that is NaN, the result is NaN.
Use a one-in-one addition or subtraction operation (+, -, plus or negative sign) for non-numeric values. If the value cannot be converted to a numeric value (converted using Number() method), NaN is returned.
var s1 = "01", s2 = "1.1", s3 = "z", b = false, o = { valueOf: function(){ return -1; } }; s1 = +s1; //1 + to -: -1s2 = +s2; //1.1 -1.1 s3 = +s3; //NaN NaN b = +b; //0 0 o = -o; //-1 1
Multiplying Infinity and 0 is equal to NaN, and multiplying the non-0 is Infinity and -Infinity, depending on the sign of the multiplier. Multiplying Infinity and Infinity equals Infinity.
var num1 =Infinity, num2 = 0, num3 = -2 ,num4 = -Infinity; alert(num1 * num2); //NaN alert(num1 * num3); //-Infinity alert(num1 * num4); //-Infinity
Zero is divided into zero and is NaN, and a non-zero number divided by zero is Infinity or -Infinity. Infinity divided by Infinity as NaN
For modulo operation, the following equation holds:
Infinity%2=NaN; 2%-Infinity=2; 0%Infinity=0; //As long as the divisor is 0, the divisor is not NaN, the result is 0Infinity%0=NaN; //The divisor can be any number. As long as the divisor is 0, the result will be NaNInfinity%Infinity=NaN
Addition operation: If both operands are strings, + becomes a string concatenation. If one is a string and the other is a numeric value, convert the numeric value to a string, and then concatenate the string. If an operand is an object, the Boolean value will first call their valueOf method. If nothing, call the toString method. Then, based on the return value type, determine whether the + sign should be connected to the string or added.
Infinity + -Infinity = NaN; var p = { valueOf: function () { return -1; } }; var num =1; var result = num +p; alert(result); // 0 Add var p = { valueOf: function () { return "not a num"; } }; var num =1; var result = num +p; alert(result); //1not a num string concatenation
Subtraction operation: Subtraction operation is very similar to addition operation, and the processing of objects is the same, so I will not explain it anymore.
Infinity - Infinity = NaN; -Infinity - -Infinity = NaN;
5. Use of relational operators
Relational operators are less than (<), greater than (>), less than or equal to (<=) and greater than or equal to (>=)
As long as there is one value, the numerical comparison will be performed, and the other is not a numerical value, and it will be converted to a numerical value. Use valueOf first for the object, and then use toString. In fact, no matter what operation the object performs, this is true. If there is valueOf, valueOf will be used to return the value, otherwise toString will be used to return the value.
Both are strings, then compare the character encoding value of the string (ASCII value)
Regarding the first one, it should be noted that when a string is a numeric value, when a string cannot be converted into a numeric value, the following situation will occur as NaN.
var result = "a" < 3; //false a converts to NaNvar result = "a" >= 3; //false Any number compared to NaN is false
6. == and ===
In JavaScript, if the two sides of the equation have different types, or only one object is included, then the comparison will be divided into two situations, and the comparison after transformation and the comparison without transformation are directly compared. == is to convert first and compare, === is to compare directly without converting. For ===, it returns false as long as the types are not equal. For ==, it is divided into the following situations:
true will be converted to 1, false will be converted to 0.
Comparing strings with numeric values, strings are converted to numeric values.
If there is only one object on both sides of the equation, this object will call valueOf to get the basic type, such as calling the toString method without the valueOf method. If both sides are objects, they will not transform.
var p = { "name":"a" }; var q = { "name":"a" } var o =p; alert(q==p); //False The addresses of objects pointed to by p and q are different, although the contents of the objects are the samealert(o==p); //true
Here are the special comparisons
null == undefined //true NaN != NaN //true NaN == NaN //false "NaN" == NaN //false undefined == 0 //false null == 0 //false
7. for-in statement
The output order of the for-in statement is unpredictable, and the order may vary depending on the browser.
When the variable to iterate is not null or undefined, the error will no longer be thrown under ECMAScript 5, but the loop body will not be executed. If you want forward compatibility, you will judge that it is not null or undefined before the loop.
8. swethc statement
Switch can use any data type.
The value of a case can be a constant, a variable, and an expression.
The switch statement uses the congruent comparison operator (===) when comparing values.
var num = 25; switch (true) { case num<0: alert("less 0"); break; case num>=0: alert("more than 0"); break; default: alert("error"); }
9 Function usage
If there is no return statement in the function or return does not have any return value, the function will return undefined.
The definition of a function is not consistent with the parameters of the function when calling it. In other words, the two parameters (formal parameters and actual parameters) have no connection. The variables provided when defining a function are only more convenient to use. Even if they are not defined, the parameters passed to the function can be obtained (through arguments[]).
function howManyArgs(){ alert(); } howManyArgs("a"); // 1 howManyArgs("a","b"); // 2 howManyArgs(); // 0
The relationship between formal parameters and arguments[] is as follows, paying attention to the difference between strict mode and non-strict mode.
function howManyArgs(ss){ arguments[0]="test"; arguments[1]="test2" alert(arguments[0]); //test alert(arguments[1]); //test2 alert(ss); //test } howManyArgs("a"); function howManyArgs(ss){ "use strict" arguments[0]="test"; arguments[1]="test2" alert(arguments[0]); //test alert(arguments[1]); //test2 alert(ss); //a } howManyArgs("a");
10. Use of function parameters
When defining a function, we will write the used parameters into the parentheses of the function, but it will be inflexible enough when there are multiple optional parameters. At this time, we can use objects to encapsulate multiple optional parameters.
function displayInfo(args){ var output = ""; if (typeof == "string"){ output += "Name: " + + "\n"; } if(typeof == "number"){ output += "Age: " + "\n"; } alert(output); } displayInfo({ name: "Nicholas", age: 29 }); displayInfo({ name: "Greg" });