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.
Related
This question already has answers here:
How does the single equal sign work in the if statement in javascript
(6 answers)
Closed 3 years ago.
This is my general code (in JavaScript):
let x = Math.floor(Math.random()×6)+1);
if (x=1){
do this
} else if (x=2){
do that
} else if.... and so on.
Whenever I test this code in a browser, only actions that could happen in the {do this} section happen. x is being locked as 1, even though it is meant to be a fair 6 sided die. Any ideas why this is happening, and how I can fix it?
The = operator in JavaScript is for assignment. x=1 will always return true because that is an assignment that will never fail. To test for equality use == (equality with conversion) or === (strict equality).
You have your if (x=1) assigning the value of 1 to x, rather than checking if it's equal to it. This results in it always returning TRUE. Using a pair of == (or ===) will solve this.
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
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.
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).
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.