Closed jsartisan closed 1 month ago
difficulty: easy title: Logical Operators type: quiz tags: javascript, logical operators
What will the following code snippet log to the console?
let a = 1; let b = ""; let c = 0; console.log(a && b && c); console.log(a || b || c);
The console output will be:
"" 1
In the code snippet, we are using the logical AND (&&) and logical OR (||) operators to evaluate expressions involving variables a, b, and c.
&&
||
a
b
c
Logical AND (&&) Operation:
a && b && c
1
""
0
console.log(a && b && c);
Logical OR (||) Operation:
a || b || c
console.log(a || b || c);
Info
Question
What will the following code snippet log to the console?
Solution
The console output will be:
Explaination
In the code snippet, we are using the logical AND (
&&
) and logical OR (||
) operators to evaluate expressions involving variablesa
,b
, andc
.Logical AND (
&&
) Operation:&&
operator returns the first falsy value it encounters, or the last value if all values are truthy.a && b && c
is evaluated:a
is1
, which is truthy.b
is""
, which is falsy.c
is0
, which is falsy.a && b && c
evaluates to the first falsy value encountered, which is""
.console.log(a && b && c);
logs""
.Logical OR (
||
) Operation:||
operator returns the first truthy value it encounters, or the last value if all values are falsy.a || b || c
is evaluated:a
is1
, which is truthy.b
is""
, which is falsy.c
is0
, which is falsy.a || b || c
evaluates to the first truthy value encountered, which is1
.console.log(a || b || c);
logs1
.