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

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/

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!

Cannot call toString() on numeric constant in Javascript [duplicate]

This question already has answers here:
Calling member function of number literal
(3 answers)
Closed 6 years ago.
toString() doesn't work on numeric literal like 5.toString(), but it works on string literal ("str".toString()) or after the numeric literal is assigned to var, like var a = 5;a.toString(). Wondering why it doesn't work for the first case?
You can do this if you wrap it in parenthesis.
(5).toString();
The first dot is the decimal mark. You need a second one to access the property:
5..toString();

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 point of !! in JavaScript? [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 9 years ago.
I was just trawling through the QUnit source code (1.12.0) and came across a line that confused me. I've done a bit of googling and haven't been able to come up with a reason for it.
Source: http://code.jquery.com/qunit/qunit-1.12.0.js line 520
result = !!result;
A similar thing appears further on in the code, except instead of storing the result in itself, it's storing the double negated variable in JSON.
Source: http://code.jquery.com/qunit/qunit-1.12.0.js line 957
result: !!result
As ! negates, I assume !! will negate then negate again, thus ending up with exactly what you started with. In which case, what is achieved by setting a variable equal to itself, negated twice? (Or in the latter example, returning itself negated twice instead of just returning itself.)
!! is used to convert the value to the right of it to its equivalent boolean value.
Also check this related Thread.

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