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