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 (a)

preface

Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean Boolean

What about 231 &&789 operations, or expressions that participate in logical operations? Is it 231 or 789?

In logic operations, there is a very important feature: we call short-circuit operations or logical interrupts

The principle of short-circuit operation: when there are multiple expressions (values), when the expression value on the left can determine the result, it will not continue to calculate the value of the expression on the right

For example, in a company, computers in a row of stations are working with electricity. Suddenly, the line of one computer shorts, and other computers and electrical appliances on this line stop working. This is called short circuit

For example, 231 &&789 mentioned above, the expression on the left side can already determine the result, so we do not continue the operation, so what is the short-circuit operation?? I will analyze it in terms of logic and sum or two

1. Logic and

  • Syntax: expression 1&& expression 2
  • If the value of the first expression is true, expression 2 is returned
  • If the value of the first expression is false, expression 1 is returned

For example 1

console.log(231 && 789)

Knowing the syntax rules, let’s take a look at the example mentioned above to see the results

Analysis: If the value of the first expression is true, expression 2 is returned

For example 2

console.log(0 && 789)

Analysis: If the value of the first expression is false, expression 1 is returned

For example, 3

You can also add expressions

console.log(0 && 2 + 1&& 3* 9999) 

Analysis: If the value of the first expression is false, expression 1 is returned

Because the first value is zero, it shorted out, and the rest is no longer executed, so the result is expression 1, and it prints 0

In Boolean operations, if the empty or negated ones are false, the rest are true

  1. Zero (0) is false,
  2. The empty string (‘) is also false,
  3. There are also several special null, undefined, and NaN that are false

For example 4

console.log( ' ' && 2 + 1&& 3* 9999 ) 

Add an empty string to the above operation, then the empty string is false, no matter what is behind the operation, the return is an empty string, as shown in the following figure

For example 5

console.log(321 && 3* 9999)

Analysis: If the value of the first expression is true, expression 2 is returned

2. To summarize

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

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! 🌹