What is short-circuiting of logical operations ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is short-circuiting of logical operations ?

Console.log(null || "user") //-->user Console.log("agnes" || "user") //-->agnes Why ?????

27th May 2020, 11:28 AM
Divyanshu Mishra
Divyanshu Mishra - avatar
2 Answers
+ 3
It means that an evaluation is stopped as soon as the result is clear. a = true b = false a||b checks only a and stops right there, because the result ('at least one has to be true') is already determined. b&&a also stops after b, because the result ('both have to be true') already failed with b being false.
27th May 2020, 11:31 AM
HonFu
HonFu - avatar
+ 2
When you want to force each part of the condition to be executed you can use the logical operators & and |. I prepared an example which executes the different variants and prints which parts got evaluated. Finally the result for each operation. https://code.sololearn.com/WrkrxSH8H377/?ref=app
27th May 2020, 12:07 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar