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

Roman
  • 4,531
  • 10
  • 40
  • 69

2 Answers2

33

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'](); 
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • I started flipping through "JavaScript: The good parts" going "I don't remember ranges in javascript." Thanks for clearing that up. – Oscar Kilhed Feb 19 '10 at 23:22
5

Combine these:

alert(1.foo); // --> parse error
alert(1.4.foo); // --> undefined - number 1.4 doesn't have the property foo
alert(1.); // --> 1 (?)

To the conclusion:

alert(1..foo); // --> undefined
Kobi
  • 135,331
  • 41
  • 252
  • 292