Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is `java` a reserved keyword in JavaScript?

I noticed that several JavaScript IDEs (e.g. Sublime) show the word java with a syntax highlighting like it is a reserved keyword.

Also older versions of Firefox have problems with a function called java.

function java() {
    alert("This function can not be called");
}

Is this actually a keyword? If yes what is it for?

like image 217
clamp Avatar asked Dec 25 '22 11:12

clamp


2 Answers

Ultimately the answer is it is not a reserved keyword, you should be safe in using java as a variable name.

But as noted on http://www.javascripter.net/faq/reserved.htm it states that java is one of the words that should have been reserved words. And I believe the reasoning he is referring too is for Java Applet integration.

It is hard to find references of this on the web because the technology is largely deprecated. It appears that up until Firefox 16 Mozilla included Globals for Packages, java, and netscape (See 2.1.5 Deprecated Functionality: the Global Packages, java and netscape Keywords)

The only bits of this I could find are referenced in Mozilla's LiveConnect Documentation where you can see in Java in Firefox Extensions First the note

Note: The global java object has been removed in Gecko 16.0, so this page is out of date

And then the sample code block:

var aJavaList = new java.util.LinkedList();

So basically before Mozilla had deprecated the global java object (and possibly applets were on the page?), this could have been a problem.

like image 187
Scott Avatar answered Jan 05 '23 19:01

Scott


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...

http://www.javascripter.net/faq/reserved.htm

like image 25
jmail Avatar answered Jan 05 '23 19:01

jmail