ECMAScript 5 (ES5), released in December 2009, introduced several improvements to JavaScript, making code more reliable and easier to maintain.
- Improved error handling and coding practices while adding methods for working with arrays and strings.
- Introduced native JSON support and enhanced date handling and serialization.
- Provided greater control over object properties and behavior through new object methods.
1. Strict Mode ("use strict")
Strict mode (use strict) enables stricter parsing and error handling in JavaScript. It helps detect common mistakes, such as assigning a value to an undeclared variable.
Syntax:
"use strict";
// Strict mode applies to this script.Example:
"use strict";
function strictFunction(a, b) {
result = a + b;
return result;
}
console.log(strictFunction(5, 10));
Output
ReferenceError: result is not defined2. String.prototype.trim()
The trim() method removes whitespace from the beginning and end of a string without modifying the original string.
Syntax:
string.trim();Example:
var str = " GeeksforGeeks ";
var result = str.trim();
console.log(result);
Output
GeeksforGeeks
3. Multiline Strings
ES5 does not support template literals. A string can continue across source lines using a backslash immediately followed by a line terminator. The line break is not included in the resulting string. String concatenation is often clearer.
Example:
var str = "GeeksforGeeks " +
"is a computer science " +
"portal.";
console.log(str);
Output
GeeksforGeeks is a computer science portal.
4. Array Methods
ES5 introduced several array methods that simplify array iteration, transformation, filtering, and value checking.
- Array.isArray(): Checks whether a value is an array.
- forEach(): Executes a callback for each array element.
- map(): Creates a new array from callback results.
- filter(): Returns a new array containing elements that pass a test.
- reduce() and reduceRight(): Combine array elements into a single result.
- every() and some(): Check whether all or at least one element satisfies a condition.
- indexOf() and lastIndexOf(): Find the first or last occurrence of a value.
Example:
var numbers = [1, 2, 3, 4, 5];
console.log(Array.isArray(numbers));
var doubled = numbers.map(function (number) {
return number * 2;
});
var evenNumbers = numbers.filter(function (number) {
return number % 2 === 0;
});
var sum = numbers.reduce(function (total, number) {
return total + number;
}, 0);
console.log(doubled);
console.log(evenNumbers);
console.log(sum);
Output
true [ 2, 4, 6, 8, 10 ] [ 2, 4 ] 15
5. JSON.parse()
The JSON.parse() method converts a valid JSON string into a JavaScript value, such as an object or array. It also supports an optional reviver function to transform parsed values.
Syntax:
JSON.parse(text, reviver);Example:
var jsonString = '{"name":"Alen","age":21,"city":"Noida"}';
var result = JSON.parse(jsonString);
console.log(result.name);
console.log(result.age);
console.log(result.city);
Output
Alen 21 Noida
6. JSON.stringify()
The JSON.stringify() method converts a JavaScript value into a JSON string. It supports optional arguments for filtering properties and formatting the output.
Syntax:
JSON.stringify(value, replacer, space);Example:
var person = {
name: "GeeksforGeeks",
established: 2009,
location: "Noida"
};
var result = JSON.stringify(person);
console.log(result);
Output
{"name":"GeeksforGeeks","established":2009,"location":"Noida"}
7. Date Methods
ES5 introduced date methods that simplify timestamp retrieval and standardized date serialization.
- Date.now(): Returns milliseconds elapsed since January 1, 1970, UTC.
- toISOString(): Converts a date to an ISO 8601 string in UTC.
- toJSON(): Returns the ISO-formatted string for a valid date object.
Example:
var currentDate = new Date();
console.log("Timestamp:", Date.now());
console.log("ISO String:", currentDate.toISOString());
console.log("JSON Date:", currentDate.toJSON());
Output
Timestamp: 1791625049488 ISO String: 2026-10-10T09:37:29.488Z JSON Date: 2026-10-10T09:37:29.488Z
Note: The timestamp and date strings vary depending on when the code runs.
8. Property Getters and Setters
ES5 supports getters and setters in object literals. A getter defines behavior when a property is read, while a setter defines behavior when a property is assigned a value.
Example:
var person = {
firstName: "John",
lastName: "Doe",
get fullName() {
return this.firstName + " " + this.lastName;
},
set fullName(name) {
var parts = name.split(" ");
this.firstName = parts[0];
this.lastName = parts[1];
}
};
console.log(person.fullName);
person.fullName = "Jane Smith";
console.log(person.firstName);
console.log(person.lastName);
Output
John Doe Jane Smith
9. Object Property Methods
ES5 introduced methods for defining property attributes, inspecting properties, and controlling whether objects can be extended or modified.
Method | Description |
|---|---|
Defines or modifies a property's attributes. | |
Defines multiple properties on an object. | |
Returns the descriptor of an own property. | |
Returns an array of an object's own enumerable string-keyed property names. | |
Returns all own string-keyed property names, including non-enumerable ones. | |
Prevents adding new properties to an object. | |
Checks whether an object can be extended. | |
Prevents adding or removing properties and makes existing properties non-configurable. | |
Checks whether an object is sealed. | |
Prevents adding or removing properties and changing existing data properties. | |
Checks whether an object is frozen. |
Example:
var person = {};
Object.defineProperty(person, "name", {
value: "Alen",
writable: false,
enumerable: true,
configurable: false
});
Object.defineProperties(person, {
age: {
value: 21,
writable: true,
enumerable: true,
configurable: true
},
city: {
value: "Delhi",
writable: true,
enumerable: true,
configurable: true
}
});
console.log(Object.keys(person));
console.log(Object.getOwnPropertyNames(person));
Object.preventExtensions(person);
console.log(Object.isExtensible(person));
Object.seal(person);
console.log(Object.isSealed(person));
Object.freeze(person);
console.log(Object.isFrozen(person));
console.log(person);
Output
[ 'name', 'age', 'city' ]
[ 'name', 'age', 'city' ]
false
true
true
{ name: 'Alen', age: 21, city: 'Delhi' }