Question about priority of logical operatorsWhich equals operator (== vs ===) should be used in JavaScript comparisons?Is there a “null coalescing” operator in JavaScript?What is the !! (not not) operator in JavaScript?typeof !== “undefined” vs. != nullHow do I check if string contains substring?parseInt(null, 24) === 23… wait, what?Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?Logical operator in a handlebars.js #if conditionalWhy use Redux over Facebook Flux?Why does OR operator have so high priority
Is random forest for regression a 'true' regression?
How did the horses get to space?
Why did the soldiers of the North disobey Jon?
I recently started my machine learning PhD and I have absolutely no idea what I'm doing
Assembly writer vs compiler
Will consteval functions allow template parameters dependent on function arguments?
Is there any good reason to write "it is easy to see"?
How to handle professionally if colleagues has referred his relative and asking to take easy while taking interview
How will the lack of ground stations affect navigation?
Why did Varys remove his rings?
What was Varys trying to do at the beginning of S08E05?
What metal is most suitable for a ladder submerged in an underground water tank?
Getting a similar picture (colours) on Manual Mode while using similar Auto Mode settings (T6 and 40D)
Why are lawsuits between the President and Congress not automatically sent to the Supreme Court
Developers demotivated due to working on same project for more than 2 years
How to describe a building set which is like LEGO without using the "LEGO" word?
Is there any deeper thematic meaning to the white horse that Arya finds in The Bells (S08E05)?
Why did the metro bus stop at each railway crossing, despite no warning indicating a train was coming?
Were any of the books mentioned in this scene from the movie Hackers real?
Could a space colony 1g from the sun work?
What is the status of the Lannisters after Season 8 Episode 5, "The Bells"?
Does the Rogue's Reliable Talent feature work for thieves' tools, since the rogue is proficient in them?
tikz drawing rectangle discretized with triangle lattices and its centroids
Understanding Python syntax in lists vs series
Question about priority of logical operators
Which equals operator (== vs ===) should be used in JavaScript comparisons?Is there a “null coalescing” operator in JavaScript?What is the !! (not not) operator in JavaScript?typeof !== “undefined” vs. != nullHow do I check if string contains substring?parseInt(null, 24) === 23… wait, what?Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?Logical operator in a handlebars.js #if conditionalWhy use Redux over Facebook Flux?Why does OR operator have so high priority
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
The following code displays 3. Why? could you explain why?
alert( null || 2 && 3 || 4 );
Is there higher priority of && over || operator? why it doesn't display 2?
javascript
add a comment |
The following code displays 3. Why? could you explain why?
alert( null || 2 && 3 || 4 );
Is there higher priority of && over || operator? why it doesn't display 2?
javascript
1
&&
does have higher precedence, but that doesn't matter here; it would evaluate to3
under any precedence rules. Can you explain how you think it could evaluate to2
?
– Paulpro
Mar 23 at 15:11
null is false and 2 is true
– user10214830
Mar 23 at 16:20
1
@Paulpro is right.&&
evaluates to the right operand, never to the2
on its left side.2 && 3
and2 && (3 || 4)
are3
. Onlynull || 2
alone would evaluate to2
.
– Bergi
Mar 23 at 18:03
add a comment |
The following code displays 3. Why? could you explain why?
alert( null || 2 && 3 || 4 );
Is there higher priority of && over || operator? why it doesn't display 2?
javascript
The following code displays 3. Why? could you explain why?
alert( null || 2 && 3 || 4 );
Is there higher priority of && over || operator? why it doesn't display 2?
javascript
javascript
asked Mar 23 at 14:34
user10214830
1
&&
does have higher precedence, but that doesn't matter here; it would evaluate to3
under any precedence rules. Can you explain how you think it could evaluate to2
?
– Paulpro
Mar 23 at 15:11
null is false and 2 is true
– user10214830
Mar 23 at 16:20
1
@Paulpro is right.&&
evaluates to the right operand, never to the2
on its left side.2 && 3
and2 && (3 || 4)
are3
. Onlynull || 2
alone would evaluate to2
.
– Bergi
Mar 23 at 18:03
add a comment |
1
&&
does have higher precedence, but that doesn't matter here; it would evaluate to3
under any precedence rules. Can you explain how you think it could evaluate to2
?
– Paulpro
Mar 23 at 15:11
null is false and 2 is true
– user10214830
Mar 23 at 16:20
1
@Paulpro is right.&&
evaluates to the right operand, never to the2
on its left side.2 && 3
and2 && (3 || 4)
are3
. Onlynull || 2
alone would evaluate to2
.
– Bergi
Mar 23 at 18:03
1
1
&&
does have higher precedence, but that doesn't matter here; it would evaluate to 3
under any precedence rules. Can you explain how you think it could evaluate to 2
?– Paulpro
Mar 23 at 15:11
&&
does have higher precedence, but that doesn't matter here; it would evaluate to 3
under any precedence rules. Can you explain how you think it could evaluate to 2
?– Paulpro
Mar 23 at 15:11
null is false and 2 is true
– user10214830
Mar 23 at 16:20
null is false and 2 is true
– user10214830
Mar 23 at 16:20
1
1
@Paulpro is right.
&&
evaluates to the right operand, never to the 2
on its left side. 2 && 3
and 2 && (3 || 4)
are 3
. Only null || 2
alone would evaluate to 2
.– Bergi
Mar 23 at 18:03
@Paulpro is right.
&&
evaluates to the right operand, never to the 2
on its left side. 2 && 3
and 2 && (3 || 4)
are 3
. Only null || 2
alone would evaluate to 2
.– Bergi
Mar 23 at 18:03
add a comment |
4 Answers
4
active
oldest
votes
&&
operator precedence is higher than that of ||
. Thus null || 2 && 3 || 4
expression is effectively the same as null || (2 && 3) || 4
.
Let's evaluate the entire expression step by step:
null
is a falsy value, so check the next operand(2 && 3)
results in 3 which is a truthy value, it stops here and returns 3
add a comment |
Is there higher priority of && over || operator?
Yes. This is called operator precedence. You can find a table for JS operators at MDN.
Your expression is evaluated like
alert((null || (2 && 3)) || 4);
To get left-to-right evaluation, you will need to write
alert((null || 2) && 3 || 4 );
2
It still logs 3 and even if the precedence of||
and&&
were swapped it would still log 3.
– Paulpro
Mar 23 at 15:07
add a comment |
The order of evaluation is
null || (2 && 3) || 4
null || 3 || 4
3 || 4
3
logical AND &&
has a higher operator precedence than logical OR ||
.
console.log(null || 2 && 3 || 4); // 3
1
Order of evaluation doesn't matter much here. It would log3
no matter what order:(null||2)&&(3||4)
or(null||2) && 3 || 4
all evaluate to3
– Paulpro
Mar 23 at 15:09
add a comment |
The ||
operator runs next operand if it returns falsy value.
The &&
operator runs next operand if it returns truthy value.
Otherwise stop going further.
So,
- null || // false, go further
- 2 && // true, go further
- 3 || // true, stop
Thus, finally returns 3.
Edit:
If you talk about priority of the operator, then you may look at the operator precedence. Where you can see &&
operator has precedence value with 6 and ||
operator has precedence value with 5. So, &&
operator has higher order.
Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.
– Bergi
Mar 23 at 18:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314785%2fquestion-about-priority-of-logical-operators%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
&&
operator precedence is higher than that of ||
. Thus null || 2 && 3 || 4
expression is effectively the same as null || (2 && 3) || 4
.
Let's evaluate the entire expression step by step:
null
is a falsy value, so check the next operand(2 && 3)
results in 3 which is a truthy value, it stops here and returns 3
add a comment |
&&
operator precedence is higher than that of ||
. Thus null || 2 && 3 || 4
expression is effectively the same as null || (2 && 3) || 4
.
Let's evaluate the entire expression step by step:
null
is a falsy value, so check the next operand(2 && 3)
results in 3 which is a truthy value, it stops here and returns 3
add a comment |
&&
operator precedence is higher than that of ||
. Thus null || 2 && 3 || 4
expression is effectively the same as null || (2 && 3) || 4
.
Let's evaluate the entire expression step by step:
null
is a falsy value, so check the next operand(2 && 3)
results in 3 which is a truthy value, it stops here and returns 3
&&
operator precedence is higher than that of ||
. Thus null || 2 && 3 || 4
expression is effectively the same as null || (2 && 3) || 4
.
Let's evaluate the entire expression step by step:
null
is a falsy value, so check the next operand(2 && 3)
results in 3 which is a truthy value, it stops here and returns 3
edited Mar 23 at 14:48
answered Mar 23 at 14:42
abadalyanabadalyan
845613
845613
add a comment |
add a comment |
Is there higher priority of && over || operator?
Yes. This is called operator precedence. You can find a table for JS operators at MDN.
Your expression is evaluated like
alert((null || (2 && 3)) || 4);
To get left-to-right evaluation, you will need to write
alert((null || 2) && 3 || 4 );
2
It still logs 3 and even if the precedence of||
and&&
were swapped it would still log 3.
– Paulpro
Mar 23 at 15:07
add a comment |
Is there higher priority of && over || operator?
Yes. This is called operator precedence. You can find a table for JS operators at MDN.
Your expression is evaluated like
alert((null || (2 && 3)) || 4);
To get left-to-right evaluation, you will need to write
alert((null || 2) && 3 || 4 );
2
It still logs 3 and even if the precedence of||
and&&
were swapped it would still log 3.
– Paulpro
Mar 23 at 15:07
add a comment |
Is there higher priority of && over || operator?
Yes. This is called operator precedence. You can find a table for JS operators at MDN.
Your expression is evaluated like
alert((null || (2 && 3)) || 4);
To get left-to-right evaluation, you will need to write
alert((null || 2) && 3 || 4 );
Is there higher priority of && over || operator?
Yes. This is called operator precedence. You can find a table for JS operators at MDN.
Your expression is evaluated like
alert((null || (2 && 3)) || 4);
To get left-to-right evaluation, you will need to write
alert((null || 2) && 3 || 4 );
edited Mar 23 at 14:44
answered Mar 23 at 14:42
BergiBergi
387k64601928
387k64601928
2
It still logs 3 and even if the precedence of||
and&&
were swapped it would still log 3.
– Paulpro
Mar 23 at 15:07
add a comment |
2
It still logs 3 and even if the precedence of||
and&&
were swapped it would still log 3.
– Paulpro
Mar 23 at 15:07
2
2
It still logs 3 and even if the precedence of
||
and &&
were swapped it would still log 3.– Paulpro
Mar 23 at 15:07
It still logs 3 and even if the precedence of
||
and &&
were swapped it would still log 3.– Paulpro
Mar 23 at 15:07
add a comment |
The order of evaluation is
null || (2 && 3) || 4
null || 3 || 4
3 || 4
3
logical AND &&
has a higher operator precedence than logical OR ||
.
console.log(null || 2 && 3 || 4); // 3
1
Order of evaluation doesn't matter much here. It would log3
no matter what order:(null||2)&&(3||4)
or(null||2) && 3 || 4
all evaluate to3
– Paulpro
Mar 23 at 15:09
add a comment |
The order of evaluation is
null || (2 && 3) || 4
null || 3 || 4
3 || 4
3
logical AND &&
has a higher operator precedence than logical OR ||
.
console.log(null || 2 && 3 || 4); // 3
1
Order of evaluation doesn't matter much here. It would log3
no matter what order:(null||2)&&(3||4)
or(null||2) && 3 || 4
all evaluate to3
– Paulpro
Mar 23 at 15:09
add a comment |
The order of evaluation is
null || (2 && 3) || 4
null || 3 || 4
3 || 4
3
logical AND &&
has a higher operator precedence than logical OR ||
.
console.log(null || 2 && 3 || 4); // 3
The order of evaluation is
null || (2 && 3) || 4
null || 3 || 4
3 || 4
3
logical AND &&
has a higher operator precedence than logical OR ||
.
console.log(null || 2 && 3 || 4); // 3
console.log(null || 2 && 3 || 4); // 3
console.log(null || 2 && 3 || 4); // 3
edited Mar 23 at 14:47
answered Mar 23 at 14:42
Nina ScholzNina Scholz
204k16117188
204k16117188
1
Order of evaluation doesn't matter much here. It would log3
no matter what order:(null||2)&&(3||4)
or(null||2) && 3 || 4
all evaluate to3
– Paulpro
Mar 23 at 15:09
add a comment |
1
Order of evaluation doesn't matter much here. It would log3
no matter what order:(null||2)&&(3||4)
or(null||2) && 3 || 4
all evaluate to3
– Paulpro
Mar 23 at 15:09
1
1
Order of evaluation doesn't matter much here. It would log
3
no matter what order: (null||2)&&(3||4)
or (null||2) && 3 || 4
all evaluate to 3
– Paulpro
Mar 23 at 15:09
Order of evaluation doesn't matter much here. It would log
3
no matter what order: (null||2)&&(3||4)
or (null||2) && 3 || 4
all evaluate to 3
– Paulpro
Mar 23 at 15:09
add a comment |
The ||
operator runs next operand if it returns falsy value.
The &&
operator runs next operand if it returns truthy value.
Otherwise stop going further.
So,
- null || // false, go further
- 2 && // true, go further
- 3 || // true, stop
Thus, finally returns 3.
Edit:
If you talk about priority of the operator, then you may look at the operator precedence. Where you can see &&
operator has precedence value with 6 and ||
operator has precedence value with 5. So, &&
operator has higher order.
Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.
– Bergi
Mar 23 at 18:00
add a comment |
The ||
operator runs next operand if it returns falsy value.
The &&
operator runs next operand if it returns truthy value.
Otherwise stop going further.
So,
- null || // false, go further
- 2 && // true, go further
- 3 || // true, stop
Thus, finally returns 3.
Edit:
If you talk about priority of the operator, then you may look at the operator precedence. Where you can see &&
operator has precedence value with 6 and ||
operator has precedence value with 5. So, &&
operator has higher order.
Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.
– Bergi
Mar 23 at 18:00
add a comment |
The ||
operator runs next operand if it returns falsy value.
The &&
operator runs next operand if it returns truthy value.
Otherwise stop going further.
So,
- null || // false, go further
- 2 && // true, go further
- 3 || // true, stop
Thus, finally returns 3.
Edit:
If you talk about priority of the operator, then you may look at the operator precedence. Where you can see &&
operator has precedence value with 6 and ||
operator has precedence value with 5. So, &&
operator has higher order.
The ||
operator runs next operand if it returns falsy value.
The &&
operator runs next operand if it returns truthy value.
Otherwise stop going further.
So,
- null || // false, go further
- 2 && // true, go further
- 3 || // true, stop
Thus, finally returns 3.
Edit:
If you talk about priority of the operator, then you may look at the operator precedence. Where you can see &&
operator has precedence value with 6 and ||
operator has precedence value with 5. So, &&
operator has higher order.
edited Mar 23 at 18:18
answered Mar 23 at 14:37
Bhojendra RauniyarBhojendra Rauniyar
53.7k2284136
53.7k2284136
Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.
– Bergi
Mar 23 at 18:00
add a comment |
Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.
– Bergi
Mar 23 at 18:00
Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.
– Bergi
Mar 23 at 18:00
Before the edit, there was no mention of operator precedence which is necessary to answer the question. After the edit, I don't think your explanation of precedence is good - it has nothing to do with reordering.
– Bergi
Mar 23 at 18:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55314785%2fquestion-about-priority-of-logical-operators%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
&&
does have higher precedence, but that doesn't matter here; it would evaluate to3
under any precedence rules. Can you explain how you think it could evaluate to2
?– Paulpro
Mar 23 at 15:11
null is false and 2 is true
– user10214830
Mar 23 at 16:20
1
@Paulpro is right.
&&
evaluates to the right operand, never to the2
on its left side.2 && 3
and2 && (3 || 4)
are3
. Onlynull || 2
alone would evaluate to2
.– Bergi
Mar 23 at 18:03