How is the boolean statement evaluated in Javascript [duplicate] - javascript

This question already has answers here:
All falsey values in JavaScript
(4 answers)
Closed 3 years ago.
So i just found out [0] == true is false, and [0] === true is also false,
so i assumed [0] should be evaluated to false. But when I am writing
if([0]) {
console.log("hi");
}
prints "hi"
That happens because Boolean([0]) returns true. But i think it should have returned false.
What am I thinking wrong here?

JS engine interpretation is that it is a non empty array. Hence, a truthy value in If condition.

Related

returning the higher value in logical operators in javascript [duplicate]

This question already has answers here:
Logical operators in JavaScript — how do you use them?
(2 answers)
Javascript AND operator within assignment
(7 answers)
Closed 1 year ago.
I am practising the logical operators in JavaScript and fully understood the concepts, but It seems that I didn't with this equation.
const one = 1;
const two = 5;
console.log(one && two);
Why in this case it returns five and not one, shouldn't be returned the first value since both are true ?
From MDN on the && operator:
"If expr1 can be converted to true, returns expr2; else, returns
expr1."
So in this case, 1 can be converted to true, so it returns the second value, 5.
The LOGICAL && operator returns the last value if all other values are true, or else it will return the first non truthy value.
i.e. Java != JavaScript

Why isNaN(true) ( or (isNaN(false) ) is returning false? [duplicate]

This question already has answers here:
Why is isNaN(null) == false in JS?
(9 answers)
Closed 3 years ago.
Just a simple question...
Why this:
isNaN(true)
or this:
isNaN(false)
return false ?
I can understand that false is evaluate to 0 and true to 1 or something...
But is it not the purpose of isNaN(...) to return false only if a variable or a value is "exactly" a number ?
As you think, they return false, because isNaN first coerces its argument into a Number, and then it returns whether the argument is NaN.
Number(true) is 1 and Number(false) is 0, so neither of them is NaN.
To perform a stricter check, and avoid implicit coercion, call Number.isNaN instead:
Both Number.isNaN(true) and Number.isNaN(false) returns true.

In operator returns false [duplicate]

This question already has answers here:
javascript string in list returns false
(3 answers)
Closed 3 years ago.
Why this statement returns false? It's really weird
console.log("100038916831294" in ["100003748210938", "100038916831294"]);
The in operator tells you whether a value exists as a property name in an object. The property names of your array are "0" and "1".
You can use one of the Array methods to check if a value is in the array, like .indexOf() or .includes():
console.log(["100003748210938", "100038916831294"].includes("100038916831294"));
The in operator in JavaScript compares indexes or property names in arrays instead of the value itself.
For example, if we write console.log(0 in ["abc","pqr"]); it will print true. However, if we use the value, like in console.log("abc" in ["abc","pqr"]); it will print false.
You can further read about it on https://www.w3schools.com/jsref/jsref_operators.asp.

What is going on with the 'in' function in javascript? [duplicate]

This question already has answers here:
Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?
(6 answers)
Why does ("a" in ["a","b"]) yield false, and (1 in [1,2]) yield true? [duplicate]
(1 answer)
Closed 3 years ago.
Something that recently broke my code is that I naively thought that:
'+' in ['+','-',...] = true.
The only problem is that it actually evaluates to false!
Someone please help me understand what is going on here!
The in operator returns true if the specified property is in the
specified object or its prototype chain.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in

Javascript Boolean type [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does !new Boolean(false) equals false in JavaScript?
var b = new Boolean(null);
alert(b instanceof Boolean);
if(b) {
alert('cv');
alert(b.toString());
}
Why if code block is executed? Is b supposed to be a boolean type and evaluated as false?
Please explain thanks
The code block executes, because the object exists and is not undefined although it has no value currently. The point of the Boolean object in javascript is to convert non-boolean objects to the "true" or "false" value.
if you have
if( b.valueOf() );
that will evaluate the actual value of the object.
All objects are truthy, except null. Therefore, even if you write new Boolean(false) specifically, it will still be truthy.
This is why you never write new Boolean. To cast to a boolean, just use !!

Categories

Resources