Can someone explain 'short circuit evaluation' to me?

In the first line, the OR || operator stops the evaluation immediately upon seeing true , so the alert isn’t run. I don’t understand why.

true || alert("not printed");
false || alert("printed");

Sometimes, people use this feature to execute commands only if the condition on the left part is falsy.

hey , you have done a weird experiment
first thing is alert is usually used while developing web pages.
i think it does not work in normal console.
second thing is alert does not return boolean value, it returns void .
OR ( || ) operator expects two boolean values on both sides.

so it is an absolute error.
hit a like if you get it

If you follow the link of the original post, you’ll find under the “short circuit” evaluation of the OR section, pressing run, outputs the “printed” string instead of the “not printed”. I dont understand that…

OR will return the first truthy value without converting into a real Boolean. If no truthy value at all it returns the last value… So for the first line it returns true since it’s the first, let me say, truthy value. Hence stopping the alert function from running…

1 Like

yeah i got that…
i thought you were trying that in a console.
there is a weird behaviour of OR operator,
as OR operator takes two boolean values on both sides , and evaluation is from left to right.
it checks the left value first
if it is true , then it will not check the 2nd expression which is after OR operator
that is the reason alert is not run in the first case.
as in the second case first value is set to false , so it checks the other side which is an alert, since it is in a web page alert is run.
but both statements do not work in a console.

The key for me was understanding that these do not (ever) convert to a boolean value. Period. They are selectors. For the &&, if the first value is falsy, then it doesn’t care what the second one is (it has no effect) so it evaluates to the first value. If the first value is truthy, than the “truthy-falsy-ness” of the second one is a ll that matters and it evaluates to that.

Similarly, for || if the first value is truthy, then it doesn’t matter what the second one is, it just evaluates to the first. Etc.

Don’t feel bad - I’ve run into senior devs that didn’t understand how these work, thinking they always evaluate to a boolean value.

3 Likes

For OR, you could think of it like:

The second operand runs only if the first operand is falsy.
And for, AND:
The second operand runs only if the first operand is truthy.

1 Like

Oh, i got it dude, thanks… the computer basically went line by line from left to right and stops at true and goes to perform the next line and then prints the alert! Thank you.

true || alert("not printed");
false || alert("printed");

that is deep. But makes me feel good about my chances in breaking into a career of sorts

Hey, so out of curiosity, where is if OR returns the first “true” value it meets, where is that value returned too and what happens? No alert to say “Value is true”?

Hey, so out of curiosity, where is if OR returns the first “true” value it meets, where is that value returned too and what happens? No alert to say “Value is true”??

It happens. For example, when you want assign a value to a variable

const myVar = 1 || 0; //  should return 1
const myVar = true || false;  //should return true
const myVar  = false || undefined; // should return undefined
const myVar = undefined || false; // should return false

Why does the OR not stop at alert(1) and go on to the value 2 btw?

alert( alert(1) || 2 || alert(3) );

Why does an alert return undefined basically?

According to the documentation, there is no return value for alert. When there is no return value, the default value is undefined, which is falsy.

or if you are using this (deprecated) version

2 Likes