1. Non-numeric type conversion value
Number()
When using transformations:
- Undefined will be Nan.
- If the string starts with 0, the browser ignores the leading 0 and does not convert according to the octal system
- If the string starts at 0x, the browser converts to decimal by hexadecimal
- If the string has characters, except (+,-,.) is converted to Nan when hexadecimal, the string contains any non-numeric character characters returns Nan
- If it is an object conversion, the object first uses valueof () and then converts to the rule. If there is no valueof method, the ToString method is invoked and then converted.
parseInt()
When using transformations:
- Parseint ignores leading spaces until the first non-null character is parsed and, if it is a non-numeric or positive sign, returns Nan. If the number is parsed until the first non-numeric number. Note: The decimal points in the parseint are not valid numeric characters
- Parseint can recognize decimal, octal, and hexadecimal, but when parsing octal, ECMAScript 3 and ECMAScript 5 are divided, ECMAScript 3 converts 070 to 56, but ECMAScript 5 is converted to 70.
- Use the second argument of parseint
var num1 = parseint ("Ten", 2); 2 by binary parsing
var num2 = parseint ("Ten", 8); 8 by octal analysis
of var num3 = parseint ("ten"); 10 by decimal Resolution
var num4 = parseint ("ten"); 16 parsing by hexadecimal
parseFloat()
When using transformations:
The first difference between parsefloat and parseint is that it is more than parseint to parse a string that is encountering an invalid floating-point numeric character.
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 can specify a cardinality, so only the decimal value is resolved.
If the string is an integer, it returns an integer instead of a floating-point number
var num = parseFloat("2.125e7"); //31250000
2. Use ToString () to output different values
This applies to integers, and we can use ToString () to return an arbitrary integer.
var num = ten;
Alert (num.tostring ()); "Ten"
Alert (num.tostring (9)); "One"
alert (num.tostring); A
3. Bitwise operators pay attention to Nan and infinity
When using bitwise operators for Nan and infinity, both values are treated as 0来. For non-numeric application bit operators, the number () function is used first to convert the value to a value.
One thing to note is that negative numbers are unsigned to the right, and the unsigned right shift is filled with an empty space with 0来, rather than a sign that fills an empty space with the symbol bit right, so the unsigned right and the signed right shift results are the same, but the negative numbers are different. An unsigned right shift takes the binary code of a negative number as the binary of a positive number, and a negative number is represented as a complement, thus resulting in a very large difference in the result of the unsigned right shift.
var oldValue = -64;
var newvalue = OldValue >>> 5
4. Special Numerical operations
For numeric operations, if there is an operand of Nan, the result is Nan.
Use unary plus or minus operations (+,-, plus or minus) for non-numeric applications, and return Nan if the value cannot be converted to a numeric value (converted using number ()).
var S1 = "a", S2 = "1.1", S3 = "Z", B = false,
o = {
valueof:function () {
return-1;
}
};
S1 = +s1; 1 + replaced by-: -1
s2 = +s2; 1.1 -1.1
s3 = +s3; Nan nan
b = +b; 0 0
o =-O; -1 1
Infinity and 0 are equal to Nan, and non-0 numbers are multiplied by Infinity and-infinity, depending on the positive sign of the multiplier. Infinity and Infinity multiply equal to infinity.
var num1 =infinity, num2 = 0, num3 =-2, num4 =-infinity;
Alert (NUM1 * num2); NaN
Alert (NUM1 * num3); -infinity
Alert (NUM1 * num4); -infinity
0 except 0 for Nan, Zero divided by 0 for Infinity or-infinity. Infinity divided by Infinity as Nan
For modulo operations, the following equation is set up:
Infinity%2=nan;
2%-infinity=2;
0%infinity=0; As long as the divisor is 0, the divisor is not NaN, the result is 0
Infinity%0=nan; The divisor can be any number, except the number is only 0, the result is NaN
Infinity%infinity=nan
Addition operation: If two operands are strings, then + becomes a string connection. If one is a string, one is a numeric value, the numeric value is converted to a string, and then the string is concatenated, and if an operand is an object, the Boolean value calls their valueof method first, if none, in the call to the ToString method. Then, depending on the return value type, whether the + number should be concatenated or added to the string.
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 connection
Subtraction: Subtraction and addition are very similar to the processing of objects, so no longer explained.
infinity-infinity = NaN;
-infinity--infinity = NaN;
5. Use of relational operators
The relational operator is less than (<), greater than (>), less than or equal (<=), and greater than or equal to (>=)
As long as there is a numeric value, the numeric comparison is performed and the other is not a numeric value. Object first with valueof, then with ToString. In fact, the object, regardless of the operation is the case, there are valueof, then return the value with valueof, otherwise use ToString return value.
Two is a string, the character encoding value (ASCII value) of the string is compared
About the first also note that at a time when a string is a numeric value, a string cannot be converted to a value, that is, Nan appears as follows
var result = "a" < 3; False a converts to Nan
var result = "a" >= 3; False any number compared to Nan is false
6. = = and = =
In JavaScript, if the equation is of different types or contains only one object, the comparison can be divided into two situations, which are compared and not transformed directly after the transition. = = = is first converted in the comparison, = = = is not converted direct comparison. for = = =, returns false as long as the type is not equal. As for = =, it is divided into the following several situations:
True converts to 1,false to 0.
String and numeric comparisons, the string is converted to a numeric value.
If there is only one object on either side of the equation, the object calls valueof to get the base type, such as no valueof method calls the ToString method. If both sides are objects then do not transition.
var p = {
"name": "A"
};
var q = {
"name": "A"
}
var o =p;
alert (q==p); False p and Q point to the address of the object differently, although the object's content is the same as
alert (o==p); True
Here's a special comparison.
Null = = undefined //true
nan!= nan //true nan
= = Nan //false
"nan" = = Nan //false
undefined = = 0 //false
null = 0 //false
7. For-in statement
For-in statement output order is unpredictable, and the order may vary depending on the browser.
When the variable to be iterated is not null or undefined, ECMAScript 5 no longer throws an error but does not execute the loop body. If you want to be forward compatible, you will not be judged to be null or undefined before the loop.
8. SWITHC 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 strict comparison operator (= = =) when comparing values.
var num =;
Switch (true) {case
num<0:
alert ("less 0");
break;
Case num>=0:
alert ("more than 0");
break;
Default:
alert ("error");
}
9 Use of the function
The function returns undefined if there is no return statement in the function or returns with no returned value.
The definition of a function is not the same as when the function is invoked. In other words, there is no connection between the two parameters (formal parameters and arguments). The variable provided in the function definition is only convenient when used, and can be obtained by passing the arguments to the function (via arguments[]) even if not defined.
function Howmanyargs () {
alert (arguments.length);
}
Howmanyargs ("a"); 1
Howmanyargs ("A", "B"); 2
Howmanyargs (); 0
The relationship between formal parameters and arguments[] is as follows, paying attention to strict and non strict mode differences.
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 we define a function, we write the arguments we use to the parentheses of the function, but are inflexible when there are multiple optional arguments, and you can use objects to encapsulate multiple optional parameters.
function DisplayInfo (args) {
var output = "";
if (typeof Args.name = = "string") {
output = = "Name:" + args.name + "\ n";
}
if (typeof args.age = = "Number") {
output + = "Age:" Args.age + "\ n";
}
alert (output);
}
DisplayInfo ({
name: "Nicholas",
age:29
});
DisplayInfo ({
name: "Greg"
});