m-a-r-c-e-l-i-n-o
m-a-r-c-e-l-i-n-o

Reputation: 2672

Why are reserved keywords in Javascript not available as variable names?

For instance, "default" is a reserved keyword in Javascript, so I can't do this:

const default = 'does not work'

According to Mozilla, the "default" keyword is used for only two cases:

switch statement and export statement pages.

Is there a good reason, from a design or technical perspective, why it couldn't be unreserved for variables? I like to think that many of these reserved JavaScript keywords could be disambiguated based on the context in which the keyword is found, but not sure. Is it just a convenience thing or more that it is practically impossible because of "X"?

Upvotes: 1

Views: 67

Answers (1)

Ludovit Mydla
Ludovit Mydla

Reputation: 822

It's for practical reasons. Imagine you would be able to create variable

var false = 1;

and then use it

if(false) {...}

How is computer supposed to know what you mean (real false vs. your variable 1) ? That's why these words are reserved. System relies on them to have specific meaning

Upvotes: 3

Related Questions