4

yield is not a reserved word in JavaScript, yet ES6 makes it a keyword.

I thought the point of reserved words was for backwards compatibility. For example, let and const were reserved, so you couldn't use those words as variable names in your ES5 code. That ensures that your ES5 code will run fine in an ES6 engine.

...but what if you used yield as a variable name? Why was this word not reserved? And if it doesn't matter, then why were any words reserved?

6

In ECMAScript 5, yield is a strict-mode "Future Reserved Word":

7.6.1.2 Future Reserved Words

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.

...

class enum    extends super
const export  import  

The following tokens are also considered to be FutureReservedWords when they occur within strict mode code (see 10.1.1). The occurrence of any of these tokens within strict mode code in any context where the occurrence of a FutureReservedWord would produce an error must also produce an equivalent error:

implements  let      private    public  yield
interface   package  protected  static    

This means that in non-strict code, your concerns are correct: the correctness of the line var yield = 5; will differ in non-strict mode between an ES5 and ES6 implementation. However, in strict mode, the line is invariably invalid for both ES5 and ES6. This means that you should use strict mode whenever possible to maximize forward compatibility of your ES5 code.

As for why yield was not fully reserved for non-strict code, I assume this was done to bridge compatibility between ES3, which did not include yield as a FutureReservedWord (see ES3 section 7.5.3, on page 14). In order to introduce yield, the commitee had to break compatibility somewhere, either in the 3-to-5 transition or the 5-to-6 transition. However, some reserved words that were included in ES3 were downgraded to strict-mode reserved words in ES5. I do not know the rationale for that decision.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.