Why two examples' output are different about Javascript Boolean? [duplicate] - javascript

This question already has answers here:
Why does !new Boolean(false) equals false in JavaScript?
(2 answers)
Why new Boolean(false) is true? [duplicate]
(7 answers)
Closed 4 years ago.
eg1:
var boo = new Boolean(false)
document.write(boo.valueOf())//false
eg2:
var boo1 = new Boolean(new Boolean(false))
document.write(boo1.valueOf())//true
Why two examples' output are different?
By the way:
console.log((new Boolean( new Boolean(false))))//nothing
document.write(new Boolean( new Boolean(false)))//true
Why is there nothing in the console?

Objects are truthy, and when you use new Boolean, you're calling the Boolean constructor, which returns an object. When new Boolean is called with a truthy value, it results in a object whose value is true. Thus, new Boolean(new Boolean(<anything>)) will result in a Boolean with a value of true.
But just don't do this - use literal booleans or Boolean(condition) instead.

Related

How does the in operator work with sets in JavaScript? [duplicate]

This question already has answers here:
In operator issue in JavaScript
(4 answers)
Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?
(6 answers)
What is the difference between "in operator" and "includes()" for JavaScript arrays
(4 answers)
Closed 1 year ago.
Why does the following code:
'%' in new Set('%');
return false?
And is the in operator faster on a set than on an array?
The in operator returns true if the specified property is in the specified object or its prototype chain.
It does not check if something belongs to a set. You should use Set.prototype.has for that.
const obj = { someKey: 'someValue' };
'someKey' in obj; // true
'someOtherKey' in obj; // false
const set = new Set([1,2,3]);
set.has(1); // true
set.has(4); // false
Regarding performance, please refer to this question

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

Why Object() != Object() in JavaScript? [duplicate]

This question already has answers here:
Comparing objects in JavaScript
(10 answers)
Closed 8 years ago.
So the question is that two same objects are not equal in JavaScript, let's say:
Object() == Object()
or even:
[{a: 1}, {a: 2}].indexOf({a: 1}); //returns -1 not found
What's the reason of this strange behavior?
Objects are compared by reference. And two references are equal only if they point to the same object.
Objects are reference and when you compare two reference they return false.
The other answer(given by Eamon Nerbonne) here has a very relevant point:
Objects are considered equivalent when
They are exactly equal per === (String and Number are unwrapped first to ensure 42 is equivalent to Number(42))
or they are both dates and have the same valueOf()
or they are both of the same type and not null and...
they are not objects and are equal per == (catches numbers/strings/booleans)
or, ignoring properties with undefined value they have the same properties all of which are considered recursively equivalent.
Same applies for Arrays
([] === []) //returns false
And NaN is a special value as well which is never equal to itself.
NaN === NaN //False

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