Significance of !! logical expression [duplicate] - javascript

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.

Related

What really is an expression in Js? [duplicate]

This question already has answers here:
JavaScript: difference between a statement and an expression?
(11 answers)
JavaScript: declarations vs expressions vs statements
(3 answers)
What is the difference between an expression and a statement in JS?
(2 answers)
Closed 18 days ago.
I have seen a bunch of articles talking about "what is an expression in Js?" but some of them said that "an expression in Js is any piece of code that produces a value" like this one 4 + 4, so this one is of course an expression the value that this will produce is 8, but what about if we only a single value of type:x, like this: 4, they also said that this is an expression but this really doesn’t match or isn’t too accurate with the previous definition, and the other case is that if we do this x = 2, that is considered also an expression an if we move forward x is also an expression, so, what really is an expression? is there different types of expression in Js? which ones are this? what are they differences? what is the really accurate definition for this concept?, please someone with this knowledge I really appreciate if you can clarify this to me!

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

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.

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 does ~[Array].indexOf(key) do? [duplicate]

This question already has answers here:
What does a tilde do when it precedes an expression?
(5 answers)
What exactly does ~ do? [duplicate]
(3 answers)
Closed 8 years ago.
I'm using and adapting the MVC example included in the Express.js repository.
In one of the modules, there's a JavaScript construct I'm not familiar with. The intent is to iterate through keys on an object and skip a few that are "reserved", but I don't understand what is happening with the tilde from a JavaScript perspective.
for (var key in obj) {
if (~['name', 'prefix', 'engine', 'before'].indexOf(key)) continue;
}
I'm reading it's a Bitwise NOT operator, but would appreciate an explanation in layman's terms of what that means, as well as what it's doing in this particular example.
indexOf returns -1 if not found. ~-1 is 0, i.e. false. So the whole things means: if key is found in the array, do continue.

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