Why Does The || (or) And && (and) Operator In Javascript Behave Differently Than In C (returning Non Boolean Value)?
Solution 1:
The logical operators in C always evaluate to boolean values. In C, the int 1
represents true
and the int 0
represents false
. That's the reason why both the expressions, "All" && 1
and "All" || 1
, evaluate to 1
. Both of them are logically true. For clarification, consider the following program.
#include<stdio.h>intmain() {
printf("%d\n", 20 && 10); // 1printf("%d\n", 20 || 10); // 1return0;
}
In the above program, the expressions 20 && 10
and 20 || 10
still evaluate to 1
even though there is no 1
in those expressions. This makes sense because both those expressions are logically true. Hence, they evaluate to 1
which is equivalent to true
in JavaScript.
If JavaScript behaved the way C did then the expressions "All" && 10
and "All" || 10
would evaluate to the boolean value true
. However, that's not the way the logical operators behave in JavaScript. That's not to say that they are buggy.
Values in JavaScript have a notion of truthiness and falsity. For example, the values true
, "All"
, 10
, [10, 20]
, { foo: 10 }
, and x => 2 * x
are all truthy. On the other hand, the values false
, ""
, 0
, undefined
, and null
are falsy.
The logical operators of JavaScript don't always evaluate to boolean values like C does. Instead, they evaluate to one of their operands. The &&
operator evaluates to its left operand if it's falsy. Otherwise, it evaluates to the right operand. Similarly, the ||
operator evaluates to its left operand if it's truthy. Otherwise, it evaluates to the right operand.
Now, the value "All"
is truthy. Hence, "All" && 1
evaluates to the right operand (i.e. 1
) whereas "All" || 1
evaluates to the left operand (i.e. "All"
). Notice that both 1
and "All"
are truthy values, which means that they are equivalent to 1
(which represents truthiness) in C.
Hence, no. JavaScript is not buggy.
Solution 2:
I quote here from official doc :- Logical AND in JS
The logical AND (&&) operator (logical conjunction) for a set of operands is true if and only if all of its operands are true. It is typically used with Boolean (logical) values. When it is, it returns a Boolean value. However, the && operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value.
Let's take some examples,
let a = [1,2,3];
console.log( 0 && a.b ); // return 0console.log( 1 && a.b ); // return a type error.
In first console.log when JavaScript see 0
at first, JS interpreter stop and return first value.
This is happening because when JS interpreter translate first value to boolean, it evaluated to false
. We know the fact that "on any &&
operator if a single value is false
, it will return false
.
So JS interpreter here try to save some computation power by returning before, without full statement evaluation. Which is great.
The same is truth for LOGICAL OR (||) operator.
Post a Comment for "Why Does The || (or) And && (and) Operator In Javascript Behave Differently Than In C (returning Non Boolean Value)?"