Skip to main content

ES5 Features (ECMAScript 2009)

ECMAScript 5 (ES5) was a massive update to JavaScript released in 2009. It introduced strict parsing, powerful new Array methods, and native JSON support, laying the groundwork for modern JavaScript.

1. "use strict"​

Adding "use strict"; at the top of a script forces the engine to parse with stricter rules. It significantly catches silent errors, like trying to use undeclared variables (which previously defaulted to global scope).

"use strict";
x = 3.14; // Throws a ReferenceError because 'x' was not declared!

2. Array Methods​

ES5 cemented the Higher-Order Array methods that functionally manipulate sequences.

  • forEach(): Iterates through elements.
  • map(): Creates a mapped clone.
  • filter(): Returns matching logic.
  • reduce(): Accumulates elements.
  • indexOf() & lastIndexOf(): Searches indices natively.
  • isArray(): Safely checks if an object is an Array.

3. JSON Support​

Before ES5, parsing JSON strings required unsafe tools or the eval() function. ES5 introduced global JSON binding:

  • JSON.parse(): Converts a JSON String into a JavaScript Object.
  • JSON.stringify(): Converts a JavaScript Object into a strictly formatted JSON String.

4. Object Configuration (Object.defineProperty)​

Gave developers precise control over object property visibility and mutability.

const person = {};

Object.defineProperty(person, "id", {
value: 100,
writable: false, // Cannot be changed
enumerable: true // Will show up in for...in loops
});

5. Getters and Setters​

Introduced robust OOP properties native to the language objects.

const user = {
firstName: "John",
lastName: "Doe",
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
};

console.log(user.fullName); // John Doe