-1

inside conditional statement any logical operation will evaluate to a boolean value which determines whether or not the conditional block will be executed or not. Like the following statement will determine the IF statement will execute or not

if(null || 1){} // evaluates to boolean value

but the same conditional statement will evaluates to Number value when assigned to a variable.Consider this:

var x = null || 1 // x will become 1

my another question is does the order of null and 1 matters here?

what determines when a conditional operation will become Boolean or Number ?

AL-zami
  • 8,902
  • 15
  • 71
  • 130
  • 2
    the first code is not a boolean result - it's "truthy" – Jaromanda X Nov 17 '17 at 03:07
  • 1
    https://developer.mozilla.org/en-US/docs/Glossary/Truthy – epascarello Nov 17 '17 at 03:14
  • 1
    The `||` expression is evaluated *first* resulting in `1` in both cases, then *that* value is evaluated as the `if` condition. Asking about the order of the operands is a bit pointless when both are hardcoded literals and you know the result in advance, but the order definitely matters in a general sense because JS uses short-circuit evaluation. So `if (func1() || func2())` will only call the second function if the first's result is falsy. – nnnnnn Nov 17 '17 at 03:16
  • @nnnnnn but if(null || 1) and if(1||null) both evaluates to true.In what context you said that order matters? – AL-zami Nov 17 '17 at 03:21
  • but var x = null || ' ' returns x = " " // empty string. In this case both is false . why x is empty string instead of null ? – AL-zami Nov 17 '17 at 03:31
  • 1
    I've deleted my previous comment because it only covered whether the result would be truthy or falsy. To answer your last comment, what the `||` operator does is return the first operand if it is truthy, otherwise return the second operand. So if both operands are falsy the second one will be returned as in your `null || ""` example. Whereas what the `&&` operator does is return the first operand if it is falsy, otherwise return the second operand. – nnnnnn Nov 17 '17 at 04:55

2 Answers2

3

They both result in the same thing. The difference, however, is that the if statement parses the value as a boolean: the 1 returned is parsed as a boolean.

> (null || 1)
1
> Boolean(1)
true

The if(null || 1){} simply evaluates to if(1){} which in turn evaluates to if(true){} since 1 is truthy.

Thus, there is no deterministic difference, the if statement simply parses the parameter as a boolean while the variable definition merely returns the number itself.

From mozzila.org:

In JavaScript, a truthy value is a value that is considered true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false , 0 , "" , null , undefined , and NaN ). JavaScript uses type coercion in Boolean contexts

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
2

In JavaScript, 1 is "truthy", which is why the if statement is entered. Basically, (null || 1) always evaluates to 1, but if forced, we say that it's true rather than false.

jhpratt
  • 6,841
  • 16
  • 40
  • 50
  • There's nothing about being forced—`1` is intrinsically `true`, as all non-zero numbers are. – A.J. Uppal Nov 17 '17 at 03:10
  • 1
    1 is not intrinsically true, it depends on the language. See ruby, where only `true` is true - everything else (including 1), is false. – jhpratt Nov 17 '17 at 03:11
  • in javascript 1 is definitely true in an intrinsic way; intrinsic meaning essential or natural, not meaning spanning all languages. – A.J. Uppal Nov 17 '17 at 05:59
  • For example, try casting `true` to a `Number` (`Number(true)`). You get `1`. – A.J. Uppal Nov 17 '17 at 06:04