Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

📝 [JavaScript] learn to form a record, [programmer essential knowledge]

📔 today’s small knowledge — JavaScript logic interrupt (2)

JS logic interrupt (1)

Logic interrupts or short circuits

1. The logic is interrupted or

  • If the result of expression 1 is true, expression 1 is returned
  • If expression 1 results in false, expression 2 is returned

For example 1

console.log(387 || 618)

According to the rules of the above characteristics, 387 | | 618 results should be 387, run it to see how the results, not surprisingly, the console print is 387

387, as expected, returns expression 1 if expression 1 is true

For example 2

Copy the above expression and add another expression to see what happens. It’s still 387 without any accidents

console.log(387 || 618 || 123 + 456 || 110)

If expression 1 is true, then expression 1 is returned, so no matter how many expressions follow, they are interrupted and will not be executed

For example, 3

What if the example expression 1 above is zero??

console.log(0 || 618 || 123 + 456 || 110)
Copy the code

Expression 0 | | the returned result is 618, 618 (1 the result is false, if the expression is returned expression (2), then 618 | | 123 + 456 | | 110618 is for real, it returns expression 1, so the final result is 618

For example 4

So let’s look at another example, like this:

 var num = 0
 console.log(123 || num++)
 console.log(num)
Copy the code

Analysis: by “1 results if the expression is true, it returns 1 expression”, then the code. The console log (123 | | num++) must be the result is 0, then for what the output of num? Is it 1 or 0??

On the console, we can see that the printed result is 0, because the logic is broken, the following is not executed, so num does not increment, the original value is 0

So does it matter if logic breaks heavily?? Obviously, logical interruption is a very important knowledge point, learning JS must master, this in a lot of front-end interviews will often test, so you learned it?

2. To summarize

As described in the previous article, the rules within the logic interrupt and budget are characterized by

  1. If the value of the first expression is true, expression 2 is returned
  2. If the value of the first expression is false, expression 1 is returned

The rule characteristic of or operation is

  1. If the result of expression 1 is true, expression 1 is returned
  2. If expression 1 results in false, expression 2 is returned

As can be seen from the above, logical interrupts are the opposite of and or. If the value of the first expression is true, it returns expression 2 in the and operation and expression 1 in the or operation

3. Write in the back

Follow me, more content continues to output

  • CSDN
  • The Denver nuggets
  • Jane’s book

🌹 if you like, give it a thumbs up 👍🌹

🌹 feel harvest, can come to a wave of collection + attention, so as not to find you next time I 😁🌹

🌹 welcome everyone to leave a message exchange, criticism and correction, forwarding please indicate the source, thank you for your support! 🌹