What is the !! operator in javascript? [duplicate] - javascript

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the !! (not not) operator in JavaScript?
Can someone explain this ‘double negative’ trick?
Because I'm playing around with the HTML5 video possibilities, I came across getUserMedia.js, which offers cross browser support.
While investigating how the library works (and trying to get it working in a requirejs module), I found the following strange if construct:
if ( !! navigator.getUserMedia_) {
...
Double negation? What does it mean and why? Why not simple use the following?
if (navigator.getUserMedia_) {
...

Double negation !! in JavaScript simply converts values to boolean type.

!! is usually used for casting variables to boolean (enforcing the boolean context)
That's used because different types can be evaluated to false, for example undefined, null, '', etc.
If you use: !!undefined, you get:
!!undefined
!true
false
In that way you actually get the boolean value which is equals to the argument if is being evaluated in boolean context.

It's not an operator exactly, it's two of the same (!) which basically converts anything into a boolean. In other words, take the double negative of a value.

Related

Easiest way of checking the truthy of a value in javascript? [duplicate]

This question already has answers here:
Falsey values in JavaScript
(6 answers)
Convert truthy or falsy to an explicit boolean, i.e. to True or False
(2 answers)
Closed 3 years ago.
While programming with javascript, I come in some situations that I have a condition an wonder what values will pass that condition (if it will be truthy).
The solution I came up with is open the console in chrome and type if(foo) {true} and if the foo is truthy, it returns true, otherwise false.
An example of that is when I have some expression (foo) that won't return only true/false values. Inside the if it can return alot of things depending on the input (sometimes string, or number or maybe NaN).
But I fell that writing a if for that is too much.
Is there a easiest way of checking the truthy of a value in javascript?
Edit:
I'm looking for the easiest way of checking the truthy of a value, not just how to check and as I said in my question, I already do if(foo) {true} to check, but I'm looking for a easiest way
I would use a ternary operator to shorthand the if-statement.
The result is something like this:
const foo = true;
console.log(foo ? 'truthy' : 'falsy'); // returns truthy

Why is Boolean([]) true in JavaScript? [duplicate]

This question already has answers here:
Why do empty JavaScript arrays evaluate to true in conditional structures?
(7 answers)
Closed 5 years ago.
I am going through JavaScript: The Definitive Guide. In it it says
Boolean([]) // => true
But I don't understand the logic behind this. Why is the boolean of an empty array true?
The ECMAScript specification defines how values are cast to booleans, per the abstract ToBoolean operation: https://www.ecma-international.org/ecma-262/6.0/#sec-toboolean
That operations includes a single entry for object input:
Object: Return true.
Thus, when you supply any object to Boolean, including an array (even an empty one), you'll get a true value back,
Array is considered as an object, even if it's empty. That's why the Boolean has a value, means it's true.
Only false, null or undefined are values which will return false.
JavaScript (and other languages) have a concept of 'truthy' and 'falsey' values.
You said you're from a C++ background, so we can make it analogous to something like this in C++:
if (ptr) { }
which is falsey if ptr is null, and truthy otherwise.
It just so happens that in JavaScript, arrays - even empty ones, among many other things - are considered to be truthy.

Not not (!!) inside if condition [duplicate]

This question already has answers here:
Why is the !! preferable in checking if an object is true? [duplicate]
(3 answers)
Closed 7 years ago.
Note: it's actually a duplicate of What is the difference between if(!!condition) and if(condition)
While I understand what the !! means (double not), for this same reason it doesn't make sense to me its use in the MDN documentation:
if (!!window.Worker) {
...
}
Isn't this exactly the same as this for this situation?
if (window.Worker) {
...
}
The casting to boolean makes no sense for me since the if will only be executed if the window.Worker exists. To say that it's True or Object for an if() conditional (I think) is the same.
So, why is the !! used here? Or, why is the window.Worker casted to boolean inside an if()?
Yes it is exactly the same. Nothing to add.
It might have been used to emphasize that the window.Worker property - expected to be a function - is cast to a boolean for detecting its presence, instead of looking like a forgotten () call. Regardless, it is now gone.

Inverse comparison/equals arguments [duplicate]

This question already has answers here:
Why Constant first in an if condition [duplicate]
(2 answers)
Checking for null - what order? [duplicate]
(8 answers)
Closed 8 years ago.
I saw many times in open source projects that folks write something like that:
if("" !== foo) {
// ...
}
Why on earth do they do that? I mean you are checking if foo's value is empty string or not. I understand that "" !== foo and foo !== "" means exactly the same. But what's the reason to write tricky and less obvious code?
I'm personally not a fan of this style, either; however, the style stems from this:
if (x = "") { // **oops**, meant to do "==" but ended up assigning to "x"
// ...
}
The "if" statement here will cause an unintended side-effect. There are a number of languages in which side effects and implicit conversions to boolean make it easy to shoot oneself in the foot this way (JavaScript and C++ come to mind). To avoid this, some have the convention of inverting the ordering, since assigning to a constant will produce a more immediate and obvious error. I'm personally not a fan of this, because it makes the code read less like English ("if x equals the empty string" reads more naturally than "if empty string equals x"), and better tooling (linters, source code analyzers, etc.) can catch these just as effectively.

Why use void(0)? [duplicate]

This question already has answers here:
What does "javascript:void(0)" mean?
(14 answers)
Closed 9 years ago.
Let's assume for a moment that you must create a JavaScript link that doesn't have a meaningful href. (I know that this practice is questionable.) In this case, why do so many people use...
My link
Knowing that void(0) evaluates to undefined, can I simply use the following logic?
My link
Why people use void(x) instead of undefined?
Well both would work but undefined is a reserved variable and its value can be changed:
undefined = true;
This will give true instead of undefined.
Where as void() is a keyword which always returns undefined. Whatever you place inside the keyword:
void('return false plox'); //will return false
More info on this topic here: What does `void 0` mean?
jsFiddle
Note that <a href="#"> is not the same as it still acts as a link and will redirect you, where as the previous methods will cancel the event(similar to event.preventDefault).
Update
Since ECMAScript 5, the global undefined variable is no longer directly editable (See for example Mozilla docs). It now simply shadows the global variable as some have noted.
There are three differences,
void evaluates the given expression and then returns the undefined
window.undefined is writable whereas void operator will always return undefined
void has fewer characters and results in smaller code, if you are using lot of them
Also, if you are using void to return undefined then you can simply use void 0, which is equivalent to void(0).

Categories