There are 4 logical operators in JavaScript:
|| (OR)&& (AND)! (NOT)?? (Nullish Coalescing)They can be applied to values of any type with results of any type.
Every value in JavaScript has an inherent boolean value: truthy or falsy.
nullundefinedfalse0, -0, NaN0n'', "", ``true[], empty object{}, and empty function () {}OR ||operand1 || operand2operand1 || operand2 || operand3 and so onOR: If any operand is truthy, then it's truthy.
Return value: first truthy or last falsy when there's no truthy.
Operator || does the following:
true, it stops and returns the original value of the operand.false, it returns the original value of the last operand.boolean1 || boolean2true || true // true true || false // true false || true // true false || false // false
true || true // true true || 5 // true (first truthy) 5 || true // 5 (first truthy) "A" || 5 // "A" (first truthy) true || false // true false || 5 // 5 (first truthy) 5 || false // 5 (first truthy) "A" || 0 // "A" (first truthy) false || false // false false || 0 // 0 (last falsy) 0 || false // false (last falsy) "" || 0 // 0 (last falsy) 0 || "" || null // null (last falsy)
Let's think about a personalized greeting alert incorporating the guest's name. Name might be inputted as first name, last name, nickname, or any combination, or none. We just want to display any 1 of them, or address the person as "Pal" if none was inputted.
let firstName;
let lastName;
let nickName = "Kitten";
alert(`Greetings, ${firstName || lastName || nickName || "Pal"}!`);
// "Greetings, Kitten!"
// If none was inputted: "Greetings, Pal!"
Short-circuit evaluation refers to how || will process the argument until first truthy value is reached, and exit with that as the return value, without ever see the rest of the argument. Then safely we can place, not just a default value, but an action/command to execute in the case no truthy value is found.
var cat = {
name: 'Kitten',
age: 3,
job: 'fishercat'
}
console.log(cat.job || 'unemployed'); // fishercat
cat.job || alert ('unemployed'); // (alert is not triggered)
var cat = {
name: 'Kitten',
age: 3,
}
console.log(cat.job || 'unemployed'); // unemployed
cat.job || alert ('unemployed'); // alert: "unemployed"
AND &&operand1 && operand2operand1 && operand2 && operand3 and so onAND: Only if all the operands are truthy, it will be truthy.
Return value: first falsy or last truthy when there's no falsy.
Operator && does the following:
false, it stops and returns the original value of the operand.true, it returns the original value of the last operand.boolean1 && boolean2true && true // true true && false // false false && true // false false && false // false
true && true // true true && 5 // 5 (last truthy) 5 && true // true (last truthy) "A" && 5 // 5 (last truthy) true && false // false false && 5 // false (first falsy) 5 && false // false (first falsy) "A" && 0 // 0 (first falsy) false && false // false false && 0 // false (first falsy) 0 && false // 0 (first falsy) "" && 0 // "" (first falsy) null && 0 && "" // null (first falsy)
NOT !!operandOperator NOT accept a single operand, convert it to boolean if not already is, and return the inverse value.
!!operandDouble NOT !! inverses the boolean value twice, returning the original boolean value of the operand. This is the same with the built-in Boolean() function.
!true; // false !null; // true !!null; // false Boolean(null); // false
The order of precedences from highest to lowest:
!&&||For example, let see following codes
let x=1;
if (x > 0) alert ("X is greater than zero!");
(x > 0) && alert ("X is greater than zero!");
// Both produces alert: "X is greater than zero!"
Even though they are analogues, this can be confusing especially if the code meant to be read by others. Let's leave if for if uses and && for && uses.
?? (Nullish Coalescing)