I have been using LiveScript for quite a while now, and I have noticed that in situations where undefined
would be implicitly returned, the expression void 8
is used instead.
Naturally, I understand the use of void
, but I cannot figure out why specifically the integer 8
is used.
For an example, the following LiveScript:
x = if truthy then \success!
Will compile to:
var x;
x = truthy ? 'success!' : void 8;
The void operator evaluates an expression and returns the primitive value undefined. void 0 evaluates 0 , which does nothing, and then returns undefined . It is effectively an alias for undefined .
JavaScript void 0 means returning undefined (void) as a primitive value. You might come across the term “JavaScript:void(0)” while going through HTML documents. It is used to prevent any side effects caused while inserting an expression in a web page.
What is the void keyword? When a function is void, it means that the function returns nothing. This is similar to functions in JavaScript which return undefined explicitly, like so: function und() { return undefined } und()
The javascript:void(0) can be used when we don't want to refresh or load a new page in the browser on clicking a hyperlink.
From the documentation on LiveScript, here's their reasoning for using void
rather than undefined
:
In JavaScript, undefined can be redefined, so it is prudent to use the void operator which produces the undefined value, always. void at the top level (not used as an expression) compiles to nothing (for use as a placeholder) - it must be used as a value to compile.
As for the 8
, it's an arbitrary number, and could have been set to any other. As per discussion in the comments below, the reason for this particular arbitrary number is because LiveScript is a fork of coco, whose wiki reports:
void 8 - the number 8 was chosen because it is a Chinese lucky number.
Regardless of how the developers chose the value, broadly speaking, it's just what the LiveScript void
compiles to. There just has to be some expression evaluated by the void
call.
Most probably 8
is the favourite number of the developer (or just a random number), as whatever you put after void
operator, you'll receive pure, not overridden undefined
.
Simple test:
void 0 === void 8 => true
void 'a' === void 8 => true
void true === void 8 => true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With