javascript: expecting true or false but getting number [duplicate] - javascript

This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Logical AND (&&) and OR (||) operators
(1 answer)
Closed 6 years ago.
I have something like the following:
var val = "string";
var testVal = val && val.length;
I would expect testVal to be either true or false but it is the length of the string. Not sure why this is?

The logical && operator doesn't perform a type casting. The expression it forms is just evaluated to the left-hand operand if this operand is falsy, otherwise to the right-hand operand.
You have to explicitly convert to boolean:
var testVal = !!(val && val.length);

Related

When does x not equal x? [duplicate]

This question already has answers here:
JavaScript numeric self equality [duplicate]
(2 answers)
What is the difference between (NaN != NaN) and (NaN !== NaN)?
(5 answers)
Closed 2 years ago.
In auditing the Javascript version of 'verb', (a NURB library,) I happened across this method:
HxOverrides.cca = function(s,index) {
var x = s.charCodeAt(index);
if(x != x) return undefined;
return x;
};
I'm puzzled by the condition,
if(x != x)
When is this ever True?
In further reading, I discovered the Javascript method, "s.charCodeAt(index)" returns the Unicode value of the (index)th character in string 's'. Specifically:
If index is out of range, charCodeAt() returns NaN.
In the console, I tested:
NaN == NaN
I found this to be false. Therefore, as to the question of:
"When does x not equal x?"
the answer (at least in Javascript,) is:
"x != x when x is NaN (not a number)".

JavaScript - type conversion [duplicate]

This question already has answers here:
what does && return in javascript expression
(4 answers)
Closed 3 years ago.
> "s" && true;
true
> true && "s";
's'
I thought these two expressions would return same values.
Why not? How does it work?
See MDN:
expr1 && expr2: If expr1 can be converted to true, returns expr2; else, returns expr1.
&& evaluates to the value of the last truthy expression, so if both operands are truthy, the whole thing will evaluate to the first operand.
If the first operand is falsey, it'll evaluate to the (falsey) value of the first operand.
Similar to ||, you can think of it as evaluating to the value of the expression that determines the final value. (If evaluating the left operand provides a truthy result with ||, there's no need to evaluate the right - similarly, if the right operand produces a falsey result with &&, there's no need to evaluate the left.)

PHP "??" operator in javascript [duplicate]

This question already has answers here:
Nullish Coalescing operator in JavaScript
(5 answers)
Closed 4 years ago.
Easy question. Does exist something like php's ?? in javascript?
In php you can do condition ?? statement2
which is the same as condition? condition : statement2.
Is there something like that in javascript?
I would use the logical or operator (||)
console.log("true value" || "other")
console.log(false || "other")
This works because (source):
expr1 || expr2 Returns expr1 if it can be converted to true; otherwise, returns expr2
So, if condition is false, go with statement2.
That's as simple as condition || statement2
If condition is truthy, if will be selected. If not, statement2 will be evaluated.

Rules for Converting string and number to Boolean in JavaScript [duplicate]

This question already has answers here:
Equality of truthy and falsy values (JavaScript)
(3 answers)
All falsey values in JavaScript
(4 answers)
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 years ago.
I read this in Eloquent JavaScript book:
The rules for converting strings and numbers to Boolean values state
that 0, NaN, and the empty string ("") count as false, while all the
other values count as true.
Because of this, expressions like 0 == false and "" == false are true.
And also following these rules this expression should evaluate to true:
console.log("A" == true)
But it evaluates to false.
Why?

How "==" works in JavaScript? [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 9 years ago.
I found == is little confusing for newbies, So I want someone to explain how it works.
For example -
new String("a") == "a" and "a" == new String("a") are both true.
new String("a") == new String("a") is false.
Why?
== is called comparison/equality operator, it compares 2 values, but not their data types so for example
1 == '1' will return true, for stricter comparison, use === which will compare the data types too so 1 === '1' will return false
== is a comparison operator that means "equal to" but does not take variable typing into account.
=== is a stricter comparison operator that means "equal to and same type".
So if you have a string called numberStr with a value of 2 and an integer called numberInt with a value of 2, they will evaluate as follows:
numberStr == numberInt // evaluates to true
numberStr === numberInt // evaluates to false because types are different

Categories