What is "void 0"? [duplicate] - javascript

This question already has answers here:
What does "javascript:void(0)" mean?
(14 answers)
Closed 8 years ago.
I'm learning Javascript by reading some code, but this function really puzzles me.
hv:
function(i) {
var _this = this;
return isArr(_this) ? _this.indexOf(i) > -1 : _this[i] !== void 0;
}
This function is added to Object.prototype.
I don't quite get the void 0 at the end of the ternary expression. Can someone explain it to me?
Thanks.

The void operator is often used merely to obtain the undefined primitive value,
usually using “void(0)” (which is equivalent to “void 0”). In these cases, the
global variable undefined can be used instead (assuming it has not been assigned
to a non-default value).
in this cases, the global variable undefined can be used instead:
ie:
_this[i] !== undefined;
Jsfiddle Demo

void 0 is a way of getting undefined without fail. Some browsers allow overriding the undefined variable, but you can't override void

Related

why javascript `var undefined` is OK but `var null` throw error [duplicate]

This question already has answers here:
Reserved keywords in JavaScript
(8 answers)
Closed 6 years ago.
why does browser throw Error for var null; ?
I am reading Javascript Garden-undefined and I know undefined is a global variable different from null.
But I am just curious about that why it throws Error when dovar null;
in contrast, when var Number=123,var Boolean=123,var Object=123,var undefined=123(although useless), etc.,it's ok.
As far as I know, null is not a reserved word and keyword in JavaScript. It should be a primitive type.
How does var work exactly in relation to null?
The difference is null is a reserved identifier and undefined isn't.
From the documentation:
The literals null, true, and false cannot be used as identifiers in ECMAScript.
This is part of the grammar definition, while global variables are just things you can (usually) override.

JavaScript: what does "void 0" mean? [duplicate]

This question already has answers here:
What does `void 0` mean? [duplicate]
(3 answers)
Closed 6 years ago.
TypeScript transpiles certain code into this:
Animal.prototype.move = function (distanceInMeters) {
if (distanceInMeters === void 0) { distanceInMeters = 0; }
...
What's void 0? Is that the same trick as used for links void(0)? Why is not undefined used instead?
The void operator always evaluates as the undefined value.
The undefined variable, which defaults to holding the undefined value, can be overwritten.
The void operator evaluates the given expression and then returns undefined.
The void operator is often used merely to obtain the undefined primitive value, usually using “void(0)” (which is equivalent to “void 0”). In these cases, the global variable undefined can be used instead (assuming it has not been assigned to a non-default value).

Is there a reason to not assign a variable in the while block declaration? [duplicate]

This question already has answers here:
Why would you use an assignment in a condition?
(12 answers)
Closed 8 years ago.
Whenever I do something like this...
var obj;
while (obj = doSomething()) {
// something with obj
}
JSHint tells me warning 84| Expected a conditional expression and instead saw an assignment.. However, doing obj = doSomething() returns the value that doSomething() returns during assignment, so it makes sense to write a while loop in this fashion.
Is there a specific reason that JSHint warns me, and more importantly, are there reasons to not do this? Or can I tell JSHint to ignore those lines for that specific warning?
That warning is to make sure that you have not mistyped = instead of == or ===.
Instead, you can get the boolean value of the evaluated result, like this
while (!!(obj = doSomething())) {
The single = assigns value and is not a comparison operator. Use the below:
while (obj == doSomething()) {
// something with obj
}
Refer: http://www.w3schools.com/js/js_comparisons.asp

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

How to understand "return obj === void 0" in the source of underscore? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does “javascript:void(0)” mean?
What does void 0 mean?
In the file http://underscorejs.org/underscore.js, you can see such a code block:
// Is a given variable undefined?
_.isUndefined = function(obj) {
return obj === void 0;
};
I don't understand the void 0 part, it's very strange. How to understand it?
This is the console output
>typeof void 0
"undefined"
>void 0 === undefined
true
>"undefined".length
9
>"void 0".length
6
I think they are trying to save 3 bytes of file size ;)
Edit:
This SO answer makes more sense of using void 0, as undefined is just a property of window object and is mutable. Hence void 0 is a trusted way to generate undefined across browsers
In javascript (since 1.1), the void operator is used to evaluate an expression and return undefined.
See ECMAScript Language Specification of the void operator
So void 0 is a correct and standard way to produce undefined.
As it is an operator, no parenthesis are needed.

Categories