Javascript Boolean type [duplicate] - javascript

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 !!

Related

Is !!!foo the same as !foo? [duplicate]

This question already has answers here:
The use of the triple exclamation mark
(4 answers)
What is an exclamation point in JavaScript?
(2 answers)
Closed 3 years ago.
Knowing that !!foo gives you the Boolean value of foo, I have seen some programmers say that it's better to use !!!foo instead of !foo because you are changing it to its Boolean value first and then negating the value.
So my question is,
Is !!!foo always equals !foo? (!!!foo === !foo)
Yup. Just for clarity:
!!x === x is not generally true, but it is true if x is already a boolean: "not (not true)" is true, and "not (not false)" is false.
!foo is always a boolean; if foo is truthy, it's false, otherwise it's true.
So if you substitute !foo in for x you get that !!(!foo) === (!foo) is always true. Removing the parentheses doesn't change the meaning, so !!!foo === !foo is always true.
Which means there's no good reason to write !!!foo in actual code. Just use !foo instead.
I've never seen !!!foo used in actual code, but yes, they are always strictly equal.
The extra two exclamation marks just negate the boolean value twice, which always gives the same result.
If foo is set as boolean then !foo will be equal to !!!foo , See the example and screenshot given below
var foo = true;
console.log('foo => '+foo);
console.log('!foo => '+!foo);
console.log('!!foo => '+!!foo);
console.log('!!!foo => '+!!!foo);
console.log('!!!!foo => '+!!!!foo);
if(!!!foo === !foo){
console.log("Equals");
}else{
console.log("Not Equals");
}
While you are correct, !!!foo would equal to !foo, I've never seen JS script using !!!foo before, and I can't think of a good reason to do that. You're gonna create code smell if you write this in your own code.
In other words, don't do that.

How is the boolean statement evaluated in Javascript [duplicate]

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.

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

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.

Javascript - succinct way to assign another value to variable if first value is not truthy [duplicate]

This question already has answers here:
Is there a "null coalescing" operator in JavaScript?
(19 answers)
Closed 4 years ago.
I have a function that may return a value or may return null and I want to assign it to a variable. So at the moment I've got
someVar = (someFun())
? someFun()
: "foo";
Is there a shorterer way of doing it, where I don't have to call the function twice, or add an extra variable like:
funResult = someFun();
someVar = (funResult)
? funResult
: "foo";
Basically, something like:
someVar = someFun() | "foo"
The idiomatic way is
someVar = someFun() || "foo";
The || operator will yield the value of the left-hand expression if it's truthy. If not, it will move on to evaluate the right-hand side, and return its value.
For those unfamiliar with the term "truthy", in JavaScript it means values that are not considered by the language to be implicitly false. The implicitly false values are 0, NaN, false (of course), "", null, and undefined. Any reference to an object, even if it's completely empty, is considered to be "truthy".
In your specific example, the || approach is to be preferred because it means you only call someFun() once.

What is the purpose of using !! in JavaScript? [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 9 years ago.
I want to know the purpose of using !! in JS.
For ex:
this.enabled = function (value) {
if (arguments.length) {
enabled = !!value;
}
}
It's not related to angular
It's just a way to transform value to bool. ( according to truthy/falsy values)
There is a lot of articles about it.
examples :
!!"a" //true
!!"0" //true
!!0 //false
To be clear, this question has nothing to do with AngularJS--it is a JS syntax question.
The purpose of !! in JavaScript (and other langs) is to force a value to boolean.
Using a single ! forces it to boolean, but opposite of whether the value was "truthy" or "falsy". The second ! flips it back to be a boolean which matches the original "truthy" or "falsy" evaluation.
var a = 'a string';
var f = !a; // f is now boolean false because a was "truthy:
var t = !!a; // f is now boolean true because a was "truthy:
It's not specific to Angular, it serves to transform a non-boolean value like undefined to boolean.

Categories

Resources