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).
Related
This question already has answers here:
Is it possible to implement equal operator in ES6?
(1 answer)
How to make comparison of objects `a == b` to be true? [duplicate]
(2 answers)
Javascript: operator overloading
(9 answers)
Closed 8 months ago.
I was wondering, if there are any ways on how to compare 2 custom string objects which are instances of classes which extend String, using == doesn't work and it always returns false.
I have tried using valueOf but with no luck, the method gets never called if both operands are objects.
See an example below
class MyString extends String {
doSomething(someData) {
return doSomethingWithData(this, someData);
};
valueOf() {
console.log('valueOf called');
return this.toString();
};
};
When doing console.log(new MyString('hi') == 'hi');, I Get
valueOf called
true
But When doing console.log(new MyString('hi') == new MyString('hi'));, I Get false.
So, the valueOf gets never called and the comparison blindly returns false, is there any possible way to fix it?
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
This question already has answers here:
Should I use `void 0` or `undefined` in JavaScript [duplicate]
(3 answers)
How can I check for "undefined" in JavaScript? [duplicate]
(16 answers)
Closed 8 years ago.
is
if( typeof myVar === "undefined" )
the same as
if( myVar===void(0) )
?
And what is the best pratice, if there is one between these ? Why ?
Quoting from MDN Docs for undefined,
One reason to use typeof is that it does not throw an error if the
variable has not been defined.
// x has not been defined before
if (typeof x === 'undefined') { // evaluates to true without errors
// these statements execute
}
if(x === undefined){ // throws a ReferenceError
}
However, this kind of technique should be avoided. JavaScript is a statically scoped language, so knowing if a variable is defined can be
read by seeing whether it is defined in an enclosing context. The only
exception is the global scope, but the global scope is bound to the
global object, so checking the existence of a variable in the global
context can be done by checking the existence of a property on the
global object (using the in operator, for instance).
From the same document's void section
var x;
if (x === void 0) {
// these statements execute
}
// y has not been defined before
if (y === void 0) {
// throws a ReferenceError (in contrast to `typeof`)
}
Conclusion
So, when you use typeof to check if the variable's value is undefined, it will not throw an exception. But direct comparison with undefined or comparison with void 0 will throw an exception.
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 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.