What's !! meaning when used in the of statement? [duplicate] - javascript

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.

Related

JavaScript Type Conversion Table [duplicate]

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.

In javascript parsing a string gives me huge number, why? [duplicate]

This question already has answers here:
How to convert a String containing Scientific Notation to correct Javascript number format
(8 answers)
Closed 5 years ago.
I have a string in response "2.222222522879E12",
When I do
parseFloat("2.222222522879E12")
2222222522879
But the expected value should be 2.22,
Whats happening ?
And how can I fix it
You're going to get 2.222222522879* 10^12 with that expression as E means exponent, or to the power of
As per your edit asking how to fix:
use the Number.prototype.toPrecision() function. Here is the documentation

Significance of !! logical expression [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 7 years ago.
Maybe I am too quick in asking this question , but I was going through angular code and I found the logical expression evaluation like this :
https://github.com/angular/angular.js/blob/master/src/ng/directive/attrs.js#L362
Essentially an attribute was evaluated like :
attr.$set(attrName, !!value);
Is there a particular reason why was this done this way ?
!! is a concise way of ensuring that value will be a boolean.

What is "~" represent in this JavaScript? [duplicate]

This question already has answers here:
What does a tilde do when it precedes an expression?
(5 answers)
Closed 8 years ago.
I am not a JavaScript expert, but I'm reading the code and found out that there's a statement like this.
if(!~dssClass.indexOf("hideDiv")
What's the "~" mean in this statement?
Thanks
It's bitwise NOT: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators
"take the result of the indexOf lookup, bitwise invert it, then take the logical NOT of that"

What's the difference between double exclamation operator and Boolean() in JavaScript? [duplicate]

This question already has answers here:
Is it 100% correct to replace !!someVar with Boolean(someVar)?
(2 answers)
Closed 6 years ago.
I know that !!variable will convert variable into a boolean value and the function Boolean(), according to the ecma262 spec, will also perform a type conversion by calling ToBoolean(value).
My question is: what's the difference? Is !! better in performance than Boolean() ?
They are the same, as the ! operator will call ToBoolean() internally on its operand, and then flip that returned value, while Boolean() will call ToBoolean() internally on its argument.

Categories