3. Introduction to cycle, reference value, explicit and implicit type conversion
cycle
for() loop: for (declaration variable; judgment statement; parameter change) {}
for(var i = 0; i <= 10; i++){ //content } //It can also be written as follows, the variable declaration can be raised, and the parameter changes can be placed inside the for loop var i = 0; for(;i <= 10;){ //content i++; } for(var i = 0; 2; i++){ console.log(i)//Infinite loop }
Note: As long as the result of the judgment statement is true, the loop will be executed, and if the result is false, it will jump out of the sequence, 0 means false, that is, jump out of the loop, and other numbers represent ture
while() loop: while (condition) {}
var i = 0; for(;i <= 10;){ //content i++; }//If for is written like this, it will be similar to var i = 0; while(i <= 10){ //content i++; }
A solution to an infinite loop:
- break
- make the condition false
do{}while() is hardly used in actual development
reference value
object array function date RegExp
introduce:
Array: [ ], through index value/assignment, .length can get the length of the array
Object: var person = { key-value pair }, value/assignment via . (dot syntax)
typeof()
Usage: typeof(x) or typeof space x
The typeof operator returns a string representing the type of the unevaluated operand.
typeof can return: 'number' 'string' 'boolean' 'object' 'undefined' 'function'
The cases where the reference type 'Object' (O uppercase) is returned are:
typeof([]); typeof({}); typeof(null);//Temporarily remember typeof (reference value) -> return Object reference type
typeof(function(){}); //'function' typeof(a);//a is not declared returns 'undefined' typeof(typeof(a));//returns 'string' typeof(typeof(typeof(a)));//returns 'string' typeof Nesting can only return'string' //implicit type conversion typeof(1 - '1'); //'number' typeof('1'-'1'); //'number' var num = '123'; typeof(num); //'string'; typeof(+num);typeof(-num); //number console.log(typeof(-num) + ':' + -num); 'number:-123' var letter = 'abc'; typeof(letter); // 'string' typeof(+letter);//'number' console.log(typeof(+letter) + ':' + +letter);//'number:NaN' console.log(typeof(-letter) + ':' + -letter);//'number:NaN' console.log(typeof(+letter) + ':' + letter);//'number:abc' // All constructors except Function are of type 'object' var str = new String('String'); var num = new Number(100); typeof str; // returns 'object' typeof num; // returns 'object' var func = new Function(); typeof func; // returns 'function'
explicit type conversion
Number(x) treats the entire parameter as a whole and converts it into a number, otherwise returns NaN
Number('1') //1 Number('a12');//NaN Number('12a');//NaN Number(true) //1 Number(false) //0 Number(null) //0 Number(undefined) //NaN Number('true') //NaN typeof(NaN) //'number' typeof(Number('true')) 'number'
parseInt(string, radix) parses a string and returns a decimal integer in the specified radix, radix is an integer between 2-36, indicating the radix of the parsed string. Return Value: An integer, or NaN, is parsed from the given string.
-
string
The value to be parsed. If the argument is not a string, it is converted to a string (using the ToString abstract operation). Whitespace at the beginning of the string will be ignored.
-
radix optional
From 2 to 36, representing the base of the string. For example, specifying 16 indicates that the parsed value is a hexadecimal number. Note that 10 is not the default!
parseInt(3.14); //3 parseInt('3.14'); //3 parseInt('3.14abc');//3 parseInt('abc123');//NaN parseInt(true);parseInt(false);parseInt(null);parseInt(undefined);parseInt(NaN); //NaN var b = 16; parseInt(b, 16)//22. Treat b as a hexadecimal number, then convert it into a decimal number and return
The parseFloat() function parses an argument (converting to a string first if necessary) and returns a float.
parseFloat('3.9415926'); //3.9415926 parseFloat(3.9415926); //3.9415926 var num = parseFloat('3.9455926'); console.log(num.toFixed(2)); //3.95,toFixed rounded to retain x decimal places
The toString() method returns the string representation of the specified object.
var str = '3.14'; str.toString(); // '3.14' undefined.toString();//mistake null.toString();//mistake //toString(x) converts the number to a string of x-ary numbers var number = 16; number.toString(16); // '10'
False in Boolean(x) are:
undefined null NaN '' 0 false Six major fakes, these return false, others return true
implicit type conversion
var a = '123'; a++; //str is implicitly converted to Number console.log(a); //number 124 var a = "a"+1; //'a1' var a = '3' * 2; //6. *,/,-,% str are implicitly converted to Number // There are comparison operators, str will be converted to number, if Number(x) cannot be converted, NaN will be returned var a = '1' > 2; // false console.log(a); var a = '1' < 2; //true var a = '1' >= 1; //true var a = 1 == '1';console.log(a); //true var a = 1 != '2';console.log(a); //true //Two characters are compared and converted into ASCII, one character is compared from left to right, and the result will be displayed if there is a winner or loser var a = 'a' > 'b';console.log(a); //false var a = 'a' < 'b';console.log(a); //true var a = 'ba' > 'b';console.log(a); //ture var a = NaN == NaN //false //The result of the comparison operator is a Boolean value, and boolean will convert Number, false->0, true->1 var a = 2 > 1 >3;console.log(a); //false var a = 2 > 1 == 1;console.log(a); //true
Note: This will not perform implicit type conversion because the equality operator already needs to determine whether the data types on both sides of the equal sign are equal, and there will be no implicit conversion step.
var a = 1 === '1' //false This will not perform implicit type conversion, because the identity operator already needs to judge whether the data types on both sides of the equal sign are equal, and there will be no implicit conversion step.
var a = undefined > 0; //false var a = undefined < 0; //false var a = undefined == 0; //false console.log(a); var a = null > 0; //false var a = null < 0; //false var a = null == 0; //false console.log(a);
isNaN() To judge whether it is NaN, the parameter is first implicitly converted by Number(x) and then judged by isNaN.
Number(x) judges, if x is not a number as a whole, it returns NaN.
isNaN(NaN); //true isNaN(123); //false isNaN('123'); //false isNaN('a'); //true isNaN(null); //false isNaN(undefined); //true