Judgement statement in JS
1,if / else if / else
var num = -6; if(num>10){ num++; //=>num=num+1 num+=1 adds up 1 on its own basis }else if(num>=0 && num<=10){ num--; }else{ num+=2; } console.log(num); //=>-4
As long as one condition is true, no matter whether there are still valid conditions after it, we can no longer judge whether it is implemented.
var num = 10; if(num>5){ num+=2; }else if(num>8){ num+=3; }else{ num+=4; } console.log(num); //=>12
What can I say about the conditions?
// >= / <= / ==General comparison if(0){ //=>Whatever you write in your conditional judgment, you will always end up calculating TRUE/FALSE to see if the condition is valid (only 0/NaN/'/null/undefined is false and the rest is true if you convert other types of values to Boolean types) } if('3px'+3){ //=>In JS, + - * /% are all mathematical operations, except +, when other operators run, if they encounter a value of non-numeric type, they first convert to numeric type (Number), and then operate on it. //=>+In addition to mathematical addition, string concatenation is also used in JS (string concatenation, not mathematical addition, if strings are encountered in operations). '3px'+3 =>'3px3' //Special {}+'str'=>NaN } if('3px'-3){ '3px'-3 =>NaN }
BAT Interview Questions:
var num = parseInt('width:35.5px'); if(num==35.5){ alert(0); }else if(num==35){ alert(1); }else if(num==NaN){ alert(2); }else if(typeof num=='number'){ //=>precompute typeof num //=>In comparison alert(3);//=>alert output is'3'in string format }else{ alert(4); }
typeof
In addition to one of the ways you can detect data types in JS:
-
instanceof
-
constructor
-
Object.prototype.toString.call()
Syntax: typeof [value] =>Detect value's data type Return value: The result detected with typeof is a string containing the corresponding data type, for example: "number"/"string"/"boolen"/"undefined"/"object"/"function" Typeof null => "object" because null represents a null object pointer (does not point to any memory space) typeof detects arrays/regulars/objects and returns "objects", which means that objects cannot be subdivided in this way
Interview questions: console.log(typeof []); //=>"object" console.log(typeof typeof []); //=>typeof "object" //=>"string"
2. Ternary Operators
Syntax: Conditions? What is established: What is not established; <=>Equivalent to simple if/else judgment
var num=12; if(num>10){ num++; }else{ num--; } //=>overridden to a ternary operator num>10?num++:num--;
Exceptional case
//=>If a part of a ternary operator does not require any processing, we use null/undeifned/void 0...Place-holder is sufficient var num = 12; num>10?num++:null; //=>If more than one operation is required, we wrap it in parentheses, with each operation statement separated by commas num=10; num>=10?(num++,num*=10):null;
Think Questions
var num = 12; if(num>0){ if(num<10){ num++; }else{ num--; } }else{ if(num==0){ num++; num=num/10; } } //Rewrite to ternary operator! num>0?(num<10?num++:num--):num==0?(num++,num=num/10,console.log(num)):null;
3,switch case
A way of judging in JS
var num = '10'; if(num==10){ num++; }else if(num==5){ num--; }else{ num=0; } /* //=>n++ Is it the same as n=n+1? var n = '10'; // n = n + 1;//=>Belongs to string splicing, the result is'101' n++;//=>The result is 11. console.log(n); */ //=>Change to switch case switch(num){ case 10: num++; break; case 5: num--; break; default: num=0; } //=>switch case applies to different operations of variables (or expressions, etc.) with different values, break ing at the end of each case (ending the whole judgment) var num = 10; switch (num) { case 10: case 5: num--; break; default: num = 0; } console.log(num); //=>9 //=>Without BREAK, the following conditions will be executed regardless of whether they are valid or not; with this mechanism, we can complete some special processing, such as: if num equals 10 and 5 do the same thing, then we write together without breaking
The comparison of each case in switch case is based on the absolute equality of'==='
'10'==10 =>true equal comparison, if the left and right sides of the equal sign have different types, they will first be converted to the same data type, and then compared =>In the current case, the string'10'is converted to a number and then compared '10'===10 Absolute comparison, if the data types on both sides are different, they are not equal directly, it requires that the types and values are exactly the same before they are equal (in real projects, we should use absolute comparison more to ensure code rigor)
FOR cycle
Role: Repeat something according to a certain rule, then we need to use the cycle to handle it
/*
* Syntax composition of the FOR loop
* 1. Define initial value var i = 0
* 2. Set the conditions under which the loop is formed (condition that the loop is formed continues and the loop is not established ends) i < ary.length
* 3. Conditions will execute the contents of the loop (braces enclose the loop)
* 4. Perform step-by-step addition
*/
var ary = [12, 23, 34]; //=>Output each item: output 34 23 12 upside down //=>ary.length-1: property name (index) of the last item in the current array /* for (var i = ary.length - 1; i >= 0; i--) { console.log(ary[i]); } */ //=>Content in the output array: Output the contents of odd items /*for (var i = 0; i < ary.length; i++) { /!*i=0 First Odd Term i=1 Second even number item i=2 Third Odd Term Indexes are even, representing odd items. How can i tell if the current value of i is odd or even? 12%5:%Called a module, divide 12 by 5 to get the remainder*!/ if (i % 2 === 0) { console.log(ary[i]); } }*/ for (var i = 0; i < ary.length; i += 2) { console.log(ary[i]); }
/* * In the loop body of the FOR loop, two commonly used keywords often appear: * 1. continue: Continue * 2. break: Break or end */ /* for (var i = 0; i < 10; i++) { if (i < 5) { i++; continue;//=>End this cycle (the code following continue in the loop will no longer execute) and continue with the next cycle } if (i > 7) { i += 2; break;//=>Force the entire loop to end without any processing } i += 1; } console.log(i);//=>10 */ for (var i = 1; i <= 10; i += 2) { if (i <= 5) { i++; continue; } else { i -= 2; break; } i--; console.log(i); } console.log(i);//=>5