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

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"

Related

Why does adding object literal + array literal result in 0 in JavaScript? [duplicate]

This question already has answers here:
What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?
(5 answers)
Objects and arrays addition
(4 answers)
Closed 3 years ago.
Why is the result of the expression {}+[] 0?
It appears that the + is treated as a unary operator instead of a normal addition operator. The expression then becomes {}0. Why is this valid JavaScript and why is the result 0? Why is the object literal in the expression ({})+[] treated normally?
Note: I tried searching for a similar question in SO but it doesn't look like searching using symbols works.

~foo(arguments); What does "~" mean in this code snippet in nodejs? [duplicate]

This question already has answers here:
What does a tilde do when it precedes an expression?
(5 answers)
Closed 3 years ago.
code snippet of unirest
As you can see above, arrayIndexOf refers to a function. When invoking the arrayIndexOf function,
is it necessary to put "~" before "arrayIndexOf"? I try to change "~arrayIndexOf(value, field)" to "arrayIndexOf(value, field)". And it works as the same? Is there anything I miss?
In JavaScript, the tilde ~ Bitwise NOT operator is commonly used right
before an indexOf() to do a boolean check (truthy/falsy) on a string.
On its own, indexOf() returns the index number of a String object
passed in. So if -1 is returned it will be turned into 0 which is
falsy.
Source: https://wsvincent.com/javascript-tilde/

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'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 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.

Categories