Binary Logical Operators in JavaScript
19 min read·Jan 1, 2026
In JavaScript, when a non-boolean value is used in a boolean context, for example in a conditional statement, such as an if, this value is automatically coerced into either true or false to avoid unexpected behavior.
Any value that is considered true when evaluated in a boolean context is called truthy, and includes:
true- non-empty strings (e.g., "hello", "0", "false")
- non-zero numbers (e.g.,
1,-2.5) - arrays (even empty)
- objects (even empty)
- functions
On the other hand, any value that is considered false when evaluated in a boolean context is called falsy, and includes:
false0nullundefinedNaN- empty strings (e.g., "")
Binary logical operators
Binary logical operators are used to combine multiple boolean expressions and return their resulting combined evaluation or value.
The logical AND operator
The logical AND operator && is used to combine two or more expressions, and either returns the first expression that evaluates to false or the last expression that evaluates to true.
expression && expression [&& expression ...]
Note: In the context of conditional statements, the entire condition will evaluate to
falseif any of its composing expressions is considered falsy.
Example
Let's consider this script:
const a = true && 'John';
const b = 'Jack' && 'John';
const c = 1 && 'John';
console.log(a, b, c);
When executed, it will:
- Assign the string
"John"to theavariable astrueis a truthy expression. - Assign the string
"John"to thebvariable as"Jack"is a truthy expression. - Assign the string
"John"to thecvariable as1is a truthy expression.
Which will produce this output:
John John John
Let's consider this script:
const a = false && 'John';
const b = null && 'John';
const c = 0 && 'John';
console.log(a, b, c);
When execute, it will:
- Assign the boolean
falseto theavariable as it is a falsy expression. - Assign the value
nullto thebvariable as it is a falsy expression. - Assign the integer
0to thecvariable as it is a falsy expression.
Which will produce this output:
false null 0
Let's consider this script:
const age = 16;
const country = 'France';
let isAdult = false;
if (age >= 21 && country === 'USA') {
isAdult = true;
}
console.log(isAdult);
When executed, it will:
- Define a variable named
ageand initialize it with the integer16. - Define a variable named
countryand initialize it with the string"France". - Define a variable named
isAdultand initialize it with the booleanfalse. - Check if the value of the
agevariable is greater or equal to21and if the value of thecountryvariable equals to the string"USA". - Assign the boolean
trueto theisAdultvariable if the condition evaluates totrue. - Output the value of the
isAdultvariable.
Which will produce this output:
false
The logical OR operator
The logical OR operator || is used to combine two or more expressions, and either returns the first expression that evaluates to true or the last expression that evaluates to false.
expression || expression [|| expression ...]
Note: In the context of conditional statements, the entire condition will evaluate to
trueif any of its composing expressions is considered truthy.
Example
Let's consider this script:
const a = true || 'John';
const b = 'Jack' || 'John';
const c = 1 || 'John';
console.log(a, b, c);
When executed, it will:
- Assign the boolean
trueto theavariable as it is a truthy expression. - Assign the string "Jack" to the
bvariable as it is a truthy expression. - Assign the integer
1to thecvariable as it is a truthy expression.
Which will produce this output:
$ node script.js
true Jack 1
Example
Let's consider this script:
const a = false || 'John';
const b = null || 'John';
const c = 0 || 'John';
console.log(a, b, c);
When execute, it will:
- Assign the
"John"string to theavariable as the booleanfalseis a falsy expression. - Assign the
"John"string to thebvariable as the valuenullis a falsy expression. - Assign the
"John"string to thecvariable as the integer0is a falsy expression.
Which will produce this output:
$ node script.js
John John John
Let's consider this script:
const country = 'France';
let isEligible = false;
if (country === 'France' || country === 'England') {
isEligible = true;
}
console.log(isEligible);
When executed, it will:
- Define a variable named
countryand initialize it with the string"France". - Define a variable named
isEligibleand initialize it with the booleanfalse. - Check if the value of the
countryvariable either equals the string"France"or"England". - Assign the boolean
trueto theisEligiblevariable if the condition evaluates totrue. - Output the value of the
isEligiblevariable.
Which will produce this output:
true
The nullish coalescing operator
Similar to the ternary operator, the nullish coalescing operator ?? returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
operand ?? operand
Example
Let's consider this script:
const vegetable = null;
const fruit = 'Apple';
console.log(vegetable ?? fruit);
When executed, it will:
- Define a variable named
vegetableand initialize it with the valuenull. - Define a variable named
fruitand initialize it with the string"Apple". - Output the value of the
fruitvariable considering that the value of thevegetablevariable isnull.
Which will produce this output:
Apple
The logical NOT operator
The logical NOT operator ! is used to invert the boolean value of an expression and either returns true if it evaluates to false, and vice-versa.
!(expression)
Note: The parenthesis are optional when negating single word expressions, such as variables.
Example
Let's consider this script:
const age = 18;
let isEligible = age >= 21;
if (!isEligible) {
console.log('Is not eligible!');
}
When executed, it will:
- Define a variable named
ageand initialize it with the number18. - Define a variable named
isEligibleand initialize it with the boolean evaluation of theage >= 21expression, which in this case isfalse. - Evaluates whether the
isEligiblevariable is falsy by negating it using the NOT!operator and output the string"Is not eligible!".
Which will produce this output:
Is not eligible!
Combine logical operators
In programming, logical operators have a precedence that determines the order in which they are evaluated when combined in expressions.
!expression && expression || expression
In JavaScript, the precedence of logical operators is as follows:
- The logical NOT
!has the highest precedence. - The logical AND
&&has the second highest precedence. - The logical OR
||has the lowest precedence.
Example
Let's consider this script:
const hasDebt = false;
const isMember = true;
const hasDayPass = false;
if (!hasDebt && isMember || hasDayPass) {
console.log('Access granted');
} else {
console.log('Access denied');
}
When executed, it will:
- Define 3 boolean variables named
hasDebt,isMember, andhasDayPayrespectively initialized withfalse,true, andfalse. - Evaluate the
!hasDebt && isMemberexpression first and convert it into a boolean expression; in this casetrue. - Evaluate the complete expression, in this case
true || false. - Output the string
"Access granted".
Which will produce this output:
Access granted
Modify the precedence of logical operators
To explicit the precedence of logical operators and force certain parts of an expression to be evaluated first, regardless of the default operator precedence, you can group two or more expressions together using parentheses () as follows:
expression && (expression || expression)
Example
Let's consider this script:
const hasDebt = false;
const isMember = true;
const hasDayPass = false;
if (!hasDebt && (isMember || hasDayPass)) {
console.log('Access granted');
} else {
console.log('Access denied');
}
When executed, it will:
- Define 3 boolean variables
hasDebt,isMember, andhasDayPay. - Evaluate the
isMember || hasDayPassexpression first and convert it into a boolean expression; in this casetrue. - Evaluate the complete expression, in this case
true && true. - Output the string
"Access granted".
The ternary operator
The ternary operator ?:, which is a concise version of the if..else statement, is mostly used for assigning values or determining expressions based on a condition.
If its expression is truthy, it returns its left-hand side operand; otherwise, it returns its right-hand side operand.
(expression) ? operand : operand
Example
Let's consider this script:
const age = 21;
const drink;
if (age >= 18) {
drink = 'Beer';
} else {
drink = 'Juice';
}
console.log(drink);
Using the ternary operator allows us to rewrite it with a much shorter syntax:
const age = 21;
const drink = (age >= 18) ? 'Beer' : 'Juice';
console.log(drink);
When executed, it will:
- Define a variable named
ageand initialize it with21. - Check if the value of the
agevariable is greater or equal to18, and either assign the string"Beer"to thedrinkvariable if it evaluates totrue, or the string"Juice"if it evaluates tofalse. - Output the value of the
drinkvariable.
Which will produce this output:
Beer
Summary
Here's a summary of what you've learned in this lesson:
- Any value that is considered
truein a boolean context is called truthy. - Any value that is considered
falsein a boolean context is called falsy. - The logical AND operator
&&returns the first expression that evaluates tofalseor the last expression that evaluates totrue. - The logical OR operator
||returns the first expression that evaluates totrueor the last expression that evaluates tofalse. - The nullish coalescing operator
??returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand. - The logical NOT operator
!returnstrueif the expression evaluates tofalse, and vice-versa. - Parentheses
()are used to modify the precedence of logical operators by forcing an expression to be evaluated first. - The ternary operator
?:is a concise version of theif..elsestatement, mostly used for assigning values or determining expressions based on a condition.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery