0

I am reading operator precedence on this page. It shows "===" has higher precedence than "||" operator. If it is true, then "a === doesThisHappen()" will run first. But why I did not get console.log('This happens!')?

var a;

a = 1;

function doesThisHappen() {

    console.log('This happens!');

    return 0;
}

if (a || a === doesThisHappen()) {
    console.log('Something is there.');
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hui
  • 1
  • 1
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence This is the page for operator precedence – Hui Oct 18 '15 at 08:19
  • you should see the log – fatihk Oct 18 '15 at 08:26
  • @RonaldoMessi: *One* of them, yes; not the other. – T.J. Crowder Oct 18 '15 at 09:15
  • This question is sadly ***the*** top search engine hit for `site:stackoverflow.com operator precedence JavaScript` (for a particular search engine at a particular time). A slightly more relevant hit is *[What is the correct JavaScript operator precedence table?](https://stackoverflow.com/questions/21158960/what-is-the-correct-javascript-operator-precedence-table)*. Yes, Stack Overflow shouldn't be used this way, but still the search engine hits ought to be much more relevant. – Peter Mortensen Sep 09 '22 at 15:33
  • Though following the link that happens to be in the question leads the right place. – Peter Mortensen Sep 09 '22 at 15:54

1 Answers1

3

Order of evaluation and operator precedence are orthogonal concepts. In a || b the left side a is evaluated first no matter what the right side b contains. More over, if the left side evaluates to true the right side is not evaluated.

Joni
  • 108,737
  • 14
  • 143
  • 193