Javascript: if 'abstract' is a reserved word why I am still able to use it as an identifier?

169 Views Asked by At

I am reading a book about JavaScript where I read about reserved words, I tried all the reserved words to us as an identifier but no one is allowed except 'abstract', I am still able to use 'abstract' as an identifier even it is a reserved word! I tried Internet Explorer, Chrome, Firefox and it is working as an identifier, why is it still allowed as an identifier even it is a reserved word? Thanks

  function myFunction(a, b) {
      abstract = a+b;
   alert("Sum = "+abstract);
  }
<!DOCTYPE html>
<html>
<head>
 <title>TestScript</title>
</head>
<body>
 <button onclick="myFunction(20, 5)">Try it</button>
</body>
</html>

3

There are 3 best solutions below

2
On BEST ANSWER

There are tons of "future" reserved words which aren't actually reserved, because the don't yet do anything. Many of them will never do anything.

For example, synchronized is also a reserved word, but var synchronized = true probably works in most browsers. Same for native, boolean, transient etc.

var native, boolean, float, await, synchronized;

native = boolean = float = await = synchronized = "blah";

document.write(native, boolean, float, await, synchronized); // blahblahblahblahblah

4
On

My suspicion is that the book you're reading is using an older version of JavaScript when abstract was still a reserved keyword. According to MDN abstract is reserved as a future keyword by older ECMAScript specifications (ECMAScript 1 till 3).

Here's the list of reserved keywords as of ES6, which is the standard your browsers are likely to be conforming to (again from MDN):

break       case        class
catch       const       continue
debugger    default     delete
do          else        export
extends     finally     for
function    if          import
in          instanceof  let 
new         return      super
switch      this        throw       
try         typeof      var         
void        while       with
yield
0
On

A while ago, abstract was singled out for future use in a later version of the language. However, it is still not yet a reserved word in the current version of the language. I would suggest not using it for the sake of future compatibility

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar