Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does LiveScript use 'void 8' for undefined values?

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;
like image 731
Jim O'Brien Avatar asked May 30 '14 17:05

Jim O'Brien


People also ask

Is void same as undefined?

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 .

Why do we use void 0 in JavaScript?

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 does void in JavaScript mean?

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

Which of the following is the correct use of void 0 in JavaScript Mcq?

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.


2 Answers

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.

like image 178
Sam Hanley Avatar answered Oct 28 '22 09:10

Sam Hanley


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
like image 42
VisioN Avatar answered Oct 28 '22 10:10

VisioN