<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?
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']();
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