29. Logical ops and value evaluation #
Created Thursday 3 March 2022
- [ ] in vault
- [x] In brain memory
In JavaScript, the logical AND and OR have special meaning.
- OR evaluates to the first truthy value. So
false || 'Sanjar'
evaluates toSanjar
. If all are falsy, it returns evaluates to the last value, sofalse || undefined
evaluates toundefined
. This is because of the short circuit behavior of OR. This behavior is used at many places to set default values to variables, like so:
function f(name) {
name = name || '<Default Name here>';
}
f(); // if nothing is passed, name = undefined, so the string is returned
- AND returns the first falsy value. It returns the last value if all variables involved are truthy. So
true && 1 && 'Sanjar'
evaluates toSanjar
. Alsoundefined && true && 'Sanjar'
evaluates toundefined
. This due to the short-circuit behavior of AND.
In short AND and OR are logical operators, but they don’t necessarily evaluate toBoolean
values.
Note: In ES6+ functions, default values can be placed in params with the assignment operator, and OR is not needed.