Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `1..something` mean in JavaScript?

Tags:

javascript

<script>
1..z
</script>

This gives no syntax or runtime error. Looks like number and variable name can be any other (123..qwerty). I'm wondering what does this statement mean?

like image 748
Roman Avatar asked Feb 19 '10 22:02

Roman


1 Answers

Is not a range, the 1..z expression will simply return undefined.

Why?

The first dot ends a representation of a Numeric Literal, giving you a Number primitive:

var n = 1.;

The grammar of a Numeric Literal is expressed like this:

DecimalIntegerLiteral . DecimalDigitsopt ExponentPartopt 

As you can see the DecimalDigits part after the dot is optional (opt suffix).

The second dot is the property accessor, it will try only to get the z property, which is undefined because it doesn't exist on the Number.prototype object:

1..z; // undefined
1..toString(); // "1"

Is equivalent to access a property with the bracket notation property accessor:

1['z']; // or
1['toString'](); 
like image 176
Christian C. Salvadó Avatar answered Oct 10 '22 19:10

Christian C. Salvadó