1. Compare sizes

1. The comparison returns a Boolean value

alert( 2 > 1 ); // true (correct)
alert( 2 == 1 ); // false(wrong) 
alert( 2 != 1 ); // true (correct)
Copy the code
  • The result of the comparison can be assigned
let result = 5 > 4; // assign the result of the comparison alert( result ); // true
Copy the code

2. String comparison

A string is a letter by letter comparison

alert( 'Z' > 'A' ); // true 
alert( 'Glow' > 'Glee' ); // true 
alert( 'Bee' > 'Be' ); // true
Copy the code

3. Comparison of different types of values

When comparing different types of values, JavaScript converts the values to numbers

alert( '2' > 1 ); // true, string '2' becomes a number 2 
alert( '01' == 1 ); // true, string '01' becomes a number 1
Copy the code
  • Amazing thing
  • Because 0 over 0 prime is going to be 0
let a = 0; alert( Boolean(a) ); // false
let b = "0"; alert( Boolean(b) ); // true
alert(a == b); // true!
Copy the code

=== === =

  • Commonly used ===== is used sparingly when == null
alert( 0 === false ); // false, because the types are different
Copy the code
  • Amazing thing
  • Remember that this particular thing is worth itnullAnd each otherundefinedequal= =Is not equal to any other value
'null' becomes' 0 'and' undefined 'becomes' NaN' alert(null == undefined); // true alert( undefined > 0 ); // false (1) alert( undefined < 0 ); // false (2) alert( undefined == 0 ); // false (3)Copy the code
9 > 3 → true "app" > "pine" → false a < p" 2" > "19" → true 2 > 1 → true undefined == null → true Null == "\n0\n" → false null === +"\n0\n" → falseCopy the code

2

  • if(…) Statement evaluates the condition in parentheses and, if true, executes the code block

  • Ternary expressions (simplified if else if)

let result = condition ? value1 : value2;
Copy the code
  • You can concatenate more than one ternary, but it is usually not used this way and has poor maintainability

  • Can you print a quick question?

  • Of course, if we already know that any string other than an empty string (and that “0” is not empty) will show true in a logical context

if ("0") { alert( 'Hello' ); }
Copy the code

3. Magic operators

1、??

  • What do you mean?

a ?? B:

  • ifaIf yes, thena.
  • ifaIf not defined, thenb.
let user; alert(user ?? "Anonymous"); // Anonymous (user not defined) user no assignment let user = "John"; alert(user ?? "Anonymous"); // John (user defined) User is assigned the value select thisCopy the code

2, and | | & application

let x = 1 && 2 ?? 3; // Syntax error let x = (1 && 2) ?? 3; // Works alert(x); / / 2Copy the code

4. Loop

1, while

  • conditionIs true,codeBut it will be executed in the body of the loop
while (condition) { // code // so-called "loop body" }
Copy the code
  • Single-line text does not need curly braces
let i = 3; 
while (i) alert(i--);
Copy the code
  • do… while
do { // loop body } while (condition);
Copy the code

2, the for

  • The most common way to loop
for (begin; condition; step) { // ... loop body ... }

for (let i = 0; i < 3; i++) { // shows 0, then 1, then 2 alert(i); }
Copy the code
  • Note that there must be twoforA semicolon;. Otherwise, syntax errors will occur
  • But all three parts of “for” can be omitted
  • So what does the next one print?
for (;;) { // repeats without limits }
Copy the code
  • Continue the next loop continue… break

The continue directive is a break. It does not stop the entire loop. Instead, it stops the current iteration and forces the loop to start a new loop (if conditions permit)

  • Question: What does this print?
let i = 3; while (i) { alert( i-- ); }
Copy the code
  • Comes back to you two
let i = 0; while (++i < 5) alert( i ); // let I = 0; while (i++ < 5) alert( i ); For (let I = 0; i < 5; i++) alert( i );Copy the code

3, the switch case

  • willswitchThere is one or morecaseModule and an optional default value
  • This is also widely used in Redux. Judge and deal with different actions in reducer
switch(x) { 
case 'value1': // if (x === 'value1') 
    ... [break] 
case 'value2': // if (x === 'value2') 
    ... [break] 
default: 
    ... [break] 
}
Copy the code
// This is an example. What do you say he prints? let a = 2 + 3; switch (a) { case 3: alert( 'Too small' ); break; case 4: alert( 'Exactly! '); break; case 5: alert( 'Too big' ); break; default: alert( "I don't know such values" ); }Copy the code