ECMAScript 5 (commonly referred to as ES5) was released in 2009 and is one of the most important versions of JavaScript. Before ES6 brought major changes, ES5 was the standard for years and is still widely supported by all modern and older browsers. If you’re learning JavaScript or working with legacy code, understanding ES5 is essential.
In this tutorial, we’ll break down the key features of ES5, how they work, and why they matter.
What is ES5?
ES5 (ECMAScript 5) is the fifth edition of the ECMAScript language specification. It standardized JavaScript for modern web development and introduced several features to improve the language’s reliability, performance, and security.
1. Strict Mode
One of the most important additions in ES5 is strict mode, which makes JavaScript run in a stricter way to avoid common mistakes.
Example:
javascriptCopyEdit'use strict';
x = 10; // ReferenceError: x is not defined
Without strict mode, JavaScript would automatically create the variable x
in the global scope. With strict mode, it throws an error, helping catch bugs early.
2. New Array Methods
ES5 added several helpful methods to work with arrays. These make it easier to loop, filter, and transform data.
forEach()
Loops through each item in an array.
javascriptCopyEdit[1, 2, 3].forEach(function(num) {
console.log(num);
});
map()
Returns a new array with the results of a function on every element.
javascriptCopyEditlet doubled = [1, 2, 3].map(function(num) {
return num * 2;
});
console.log(doubled); // [2, 4, 6]
filter()
Returns a new array with elements that match a condition.
javascriptCopyEditlet even = [1, 2, 3, 4].filter(function(num) {
return num % 2 === 0;
});
console.log(even); // [2, 4]
reduce()
Reduces an array to a single value.
javascriptCopyEditlet sum = [1, 2, 3].reduce(function(total, num) {
return total + num;
}, 0);
console.log(sum); // 6
3. JSON Support
ES5 introduced native support for JSON (JavaScript Object Notation), which is widely used for data exchange.
Example:
javascriptCopyEditlet jsonString = '{"name":"Alice","age":25}';
let obj = JSON.parse(jsonString); // convert to object
console.log(obj.name); // Alice
let newString = JSON.stringify(obj); // convert to JSON string
console.log(newString);
4. Object Methods
ES5 added powerful methods to work with objects:
Object.keys(obj)
— returns an array of keysObject.create(proto)
— creates a new object with the specified prototypeObject.defineProperty(obj, key, descriptor)
— adds or changes a property with fine control
Example:
javascriptCopyEditlet person = {};
Object.defineProperty(person, 'name', {
value: 'Bob',
writable: false
});
console.log(person.name); // Bob
person.name = 'Alice';
console.log(person.name); // still Bob (cannot be changed)
5. Getter and Setter Functions
You can define custom behavior for getting or setting properties.
Example:
javascriptCopyEditlet user = {
firstName: 'John',
lastName: 'Doe',
get fullName() {
return this.firstName + ' ' + this.lastName;
}
};
console.log(user.fullName); // John Doe
6. Array isArray()
Method
A simple method to check if a variable is an array:
javascriptCopyEditconsole.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray("hello")); // false
7. Preventing Object Modifications
ES5 introduced methods to make objects more secure:
Object.freeze(obj)
— makes the object immutableObject.seal(obj)
— prevents adding or removing properties
Example:
javascriptCopyEditlet car = { brand: 'Toyota' };
Object.freeze(car);
car.brand = 'Honda'; // does nothing
console.log(car.brand); // Toyota
Why Learn ES5?
Even though newer versions like ES6+ offer better syntax and features, ES5 is still relevant because:
- It’s supported in all browsers, including old ones (like Internet Explorer).
- Many existing libraries and legacy projects use ES5.
- Understanding ES5 helps you appreciate how JavaScript evolved.
Conclusion
ES5 laid the foundation for modern JavaScript. It introduced strict mode, new array methods, JSON support, better object handling, and security features. Whether you’re learning JS from scratch or maintaining older codebases, a strong understanding of ES5 is essential.
Take time to practice these features and experiment in the browser console. You’ll build a solid understanding of core JavaScript principles that apply in both older and newer versions of the language.