Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JavaScript reserves Java keywords?

As you know, JavaScript has all Java keywords reserved. Does anyone know why? JavaScript discourages using these Java keywords, but they appear to work fine when used as identifiers.

like image 636
Tom Tucker Avatar asked Jan 31 '11 19:01

Tom Tucker


People also ask

Why are keywords reserved?

Reserved words (also called keywords) are defined with predefined meaning and syntax in the language. These keywords have to be used to develop programming instructions. Reserved words can't be used as identifiers for other programming elements like name of variable, function etc.

Why do we use keyword in JavaScript?

In JavaScript, the this keyword allows us to: Reuse functions in different execution contexts. It means, a function once defined can be invoked for different objects using the this keyword. Identifying the object in the current execution context when we invoke a method.

How many reserved keywords are there in JavaScript?

There are a total of 63 reserved words in JavaScript.


4 Answers

The story is that when they were developing JavaScript (originally called Oak I believe (apparently, I got the languages mixed up the previous statement about it's original name is incorrect.)), Netscape partnered with Sun to develop it. To entice the Java community, they wanted to make JavaScript like Java, so that Java developers would feel more comfortable with it, and that is the reason why they are so similar.

What you have to remember about designing a language is that you really only get one shot at defining keywords, without having a new version of the language break existing code. It's much easier to reserve a word at the beginning and not use it than try and reserve it later. This is especially true for things like JavaScript where the person developing the script has no control over which browser it's going to run in (this happens now i know, but it could be a lot worse). Could you imagine how madding it would be to find out that the next version of JavaScript which is adopted by the new browser suddenly won't run your application, because they reserved a new keyword?

like image 54
kemiller2002 Avatar answered Nov 01 '22 21:11

kemiller2002


Well, it's not actually Java keywords, as Javascript doesn't have anything to do with Java except borrowing the name.

The names were reserved in case they would be needed in future expansions of the language. From the ECMAScript Language Specification:

"The following words are used as keywords in proposed extensions and are therefore reserved to allow for the possibility of future adoption of those extensions..."

like image 36
Guffa Avatar answered Nov 01 '22 22:11

Guffa


Probably to avoid potential confusion that using these keywords as variable names could cause.

I mean, just imagine a Java developer trying to figure out a piece of JavaScript code with a variable or a function named public.

Even when that is possible, I'd advise against it, for the sake of clarity.

like image 42
Goran Jovic Avatar answered Nov 01 '22 20:11

Goran Jovic


Presumably the developers of JavaScript assumed that these words may be used for additional functionality in a future release of JavaScript. To prevent backwards-compatibility issues, these words were preemptively marked as reserved.

like image 28
Jason LeBrun Avatar answered Nov 01 '22 22:11

Jason LeBrun