What does ~[Array].indexOf(key) do? [duplicate] - javascript

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.

Related

How to turn strings into boolean expressions? [duplicate]

This question already has answers here:
JavaScript -- write a function that can solve a math expression (without eval)
(2 answers)
Closed 3 years ago.
I'm working on a problem generator that spits out phrases like "!(A && B) || (A || B)", but it does so in a string (I have a function that spits this out in string form). How would I convert this string expression into a same boolean expression in javascript? I've tried to use JSON.parse() but it keeps giving me an error.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval should be the function you're looking for, however read the note about the huge security risk!

~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/

The use of [...] operator in typescript [duplicate]

This question already has answers here:
What does the "..." (triple dot) notation in arrays mean? [duplicate]
(2 answers)
Closed 5 years ago.
I was going through a typescript code (with Angular 2) on website and I found following :
let episodesCopy = JSON.parse(JSON.stringify(this.episodes))
this.episodes=[...episodesCopy,this.otherEpisodes.pop()];
I want to understand whether operator ... in ...episodesCopy is a typescript operator or a javascript operator? and what exactly it does?
That is the spread operator or spread syntax. It is essentially taking an expression (usually an array) and converting it into multiple statements.

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"

Categories