This chapter lists features that are available only in ECMAScript 5. Should you have to work with older JavaScript engines, you should avoid these features or enable some of them via a library (how is described later). Note that normally, this book assumes that you are working with modern engines, which fully support ECMAScript 5.

The ECMAScript 5 specification contains the following description of its scope:

The fifth edition of ECMAScript (published as ECMA-262 5th edition)- codifies de facto interpretations of the language specification that have become common among browser implementations and- adds support for new features that have emerged since the publication of the third edition. Such features include - accessor properties, - reflective creation and inspection of objects, - program control of property attributes, - additional array manipulation functions, - support for the JSON object encoding format, and - a strict mode that provides enhanced error checking and program security.

The new

  • Strict mode (see Strict Mode)
  • Putting the following line first in a file or a function switches on the so-called strict mode that makes JavaScript a cleaner language by forbidding some features, performing more checks, and throwing more exceptions:
  • Accessors (see )
  • Getters and setters allow you to implement the getting and setting of a property via methods. For example, the following object contains a getter for the property foo:

ECMAScript 5 includes the following syntactic changes:

  • Reserved words as property keys
  • You can use reserved words (such as new and function) after the dot operator and as unquoted property keys in object literals:
  • Legal trailing commas
  • Trailing commas in object literals and array literals are legal.
  • Multiline string literals
  • String literals can span multiple lines if you escape the end of the line via a backslash.

Getting and setting prototypes (see ):

  • Object.getPrototypeOf()

Managing property attributes Property Descriptors):

  • Object.defineProperty()
  • Object.defineProperties()
  • Object.create()
  • Object.getOwnPropertyDescriptor()

Listing properties Iteration and Detection of Properties):

  • Object.keys()
  • Object.getOwnPropertyNames()

Protecting Protecting Objects):

  • Object.preventExtensions()
  • Object.isExtensible()
  • Object.isSealed()
  • Object.freeze()
  • Object.isFrozen()
  • Function.prototype.bind()

Strings (see ):

  • New method String.prototype.trim()
  • Access characters via the bracket operator […]

New Array methods (see ):

  • Array.isArray()
  • Array.prototype.filter()
  • Array.prototype.forEach()
  • Array.prototype.indexOf()
  • Array.prototype.map()
  • Array.prototype.reduce()
  • Array.prototype.some()

New Date methods (see ):

  • Date.now()
  • Date.prototype.toISOString()

Support for JSON (see ):

  • Boolean.prototype.toJSON()
  • Number.prototype.toJSON()
  • String.prototype.toJSON()
  • Date.prototype.toJSON()
  • A compatibility table by Juriy Zaytsev (“kangax”) shows how much of ECMAScript 5 is supported by various versions of various browsers.