JS series: JS ternary operator

Ternary operator Grammar: conditions? Set up...

Ternary operator

Grammar: conditions? Set up to do things: not set up to do things; < = > equivalent to a simple if/else judgment (simplified writing)

var num = 12; if(num>10){ num ++; }else { num--; } => Equivalent to a ternary operator: num > 10? num++ : num--;

exceptional case:

//=>If a part of the ternary operator is tenable, it doesn't need any processing. Let's use null / underfound / void 0... To occupy the bit var num=12; num>10?num++ : null; //=>If more than one task needs to be performed, we wrap it in parentheses and separate each statement with a comma num=10; num>10?(num++,num*=10):null;

Thinking questions:

var num =12;
if(num>0){
if(num<10){

num++;

}else {

num--;

}
}else {
if(num==0){

num++; num=num/10; }

}
Change to ternary operator:
var num=12;
num > 0 ? (num < 10 ? num ++ : num-- ) : (num--, num=num/10);

switch case

A judgment method in js. switch case is applied to different operations of variables (expressions) under different values. After each case is completed, it needs to add break (to end the whole judgment)

var num = 10; if(num==10){ num ++; }else if(num==5){ num--; }else { num=0; } //Change to switch case var num = 10; switch(num){ case 10: num++; break; case 5: num--; break; default: num=0; } > No break,The latter condition will be executed whether it is true or not; with this mechanism, we can complete some special processing, such as: if num Equal to 10 or 5, we have to do the same thing, so we don't need to add break Just (remove case10 Inside break)
  • Thinking: is n + + the same as n=n+1?
    1. var n='10';
      n = n+1;
      console.log(n) =>101
      //=>Belongs to string splicing, the result is' 101 '
      String is string concatenation, numerical value is mathematical operation, if it is n + +, it will be mathematical operation

      =Yes assignment, = = judge whether equal, = = judge absolute equal (both types and values must be equal)

'10' = = 10 = > true value is equal, type is different, equal comparison. If the types on the left and right sides of the equal sign are different, first convert the same data type, and then compare. The current case is to convert the string '10' to a number, and then compare. '10' = = 10 = > false values and types are equal. Absolute comparison: if the data types on both sides are different, you don't want to type it directly. It requires that the types and values are exactly the same before they are equal. (in real projects, to ensure the preciseness, use the absolute equal sign.)
  • The comparison of each case in switch case is based on "====" absolute equality.

example:

var num = 5; switch(num){ case 10: case 5: num--; break; default: num=0; } console.log(num); VM1668:12 4

FOR cycle

Function: to do something repeatedly according to certain rules, which needs to be handled in a circular way.

var ary = [12,23,34]; ary => { 0:12 1:23 3:34 length:3 } //Output each item in the array /* ==itar [TAB]Key auto complete cycle code for (var i=0; i<ary.length; i++){ //=>First cycle: I = 0; I < 3.. I = 1 = > ary [0] //=>The second cycle: I = 1; I < 3.. I = 2 = > ary [1] //=>The third cycle: I = 2; I < 3.. I = 3 = > ary [2] //=>The fourth cycle: I = 3; I < 3 cycle end (no cycle this time) console.log(ary['i']); } * Inverted output: var ary = [12,23,34]; //=>Output each item backwards. Flip output 34 23 12 //> ary.length ; property name (index) of the last item in the current array for (var i= ary.length - 1; i >= 0;i-- ){ console.log(ary[i]) } => 34 23 12 * Output odd var ary= [12,23,34]; for (var i=0; i<ary.length; i+=2){ //=>Output the contents of odd items in the array // i = 0 first odd term // i = 1 second even term // i = 2 third odd term // The index is even, representing odd items. If you decide whether the current i value is odd or even? //12% 5:% is called modulus, and the remainder is 12 divided by 5 console.log(ary[i]); } }
  • Syntax composition of FOR loop:

    1. Define initial value var i = 0
    2. Set cycle conditions (conditions set cycle to continue, not set cycle to end) I< ary.length
    3. If the condition is true, the content in the loop body will be executed (the loop body is wrapped in braces)
    4. Perform step accumulation
    • In the loop body of FOR loop, there are two common keywords:

      1. Continue: continue
      2. break: middle or end
      for (var i = 0; i<10;i++){ if(i<5){ i++; continue; //=>End this cycle (the code of continue in the cycle body is not executing) and continue to the next cycle } if(i>7) { i + =2; break;//=>Forced end of loop, no processing } i += 3; }

Thinking questions:
for (var i=1; i<=10; i+=2){

if(i <= 5){ i++; continue; }else { i -=2; break; } i--; console.log(i); } =>5

10 June 2020, 23:51 | Views: 4849

Add new comment

For adding a comment, please log in
or create account

0 comments