This question already has answers here:
How does JS type coercion work?
(2 answers)
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 years ago.
I have a question about type conversions. Given the following table from w3schools.com...
Why are the strings "0" and "000" converted to boolean true?
Because when you coerce a value to boolean in JavaScript, any non-blank string is true. Only blank strings are false.
The reason of it is because both "0" and "000" are strings and not numbers.
Any string which is non-empty converted to boolean is going to be true.
Related
This question already has answers here:
Empty array does not equal itself in Javascript?
(4 answers)
Closed 1 year ago.
To check if an array is empty , we have many choices like if(array.length===0) or if(array=='') but I'm wondering why if (array==[]) doesn't check if an array is empty or not. Any one has a clear explanation?
Double Equals ( == ) checks for value equality only. It inherently does type coercion. This means that before checking the values, it converts the types of the variables to match each other. and in the same way [] doesn't have any type and if(arr == []) doesn't work in javascript
This question already has answers here:
Javascript compare numbers as strings
(2 answers)
Closed 2 years ago.
When the smaller number string when compared with the larger number string - the result is true.
How is it possible?
document.write(`Why the result of '2'>'10' is ${'2'>'10'}`)
Any expert here?
You're comparing strings, so a lexical comparison is performed instead of a numerical comparison.
Lexically, 2 comes after 1.
This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
parseInt vs unary plus, when to use which?
(6 answers)
JavaScript primitive types and corresponding objects
(2 answers)
What is the difference between JavaScript object and primitive types?
(1 answer)
What is the difference between a number and a number object?
(3 answers)
Closed 3 years ago.
I see there + operator is being used to convert a string number to number and same does the Number() function ,
So i am wondering there must be good reason for + operator before string number being used in most cases over Number()
Is there any performance enhancement with + operator?
appreciate your help guys
note : don't get confuse with concatenation of string , its unary + oprator
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?
This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 7 years ago.
Such as if(!!you) , I thought we can get rid of the !! , and it's the same. Cause JavaScript will change it to Boolean automatically?
!! cast the variable to a Boolean. Similar to how you do +foo to cast it to a number.