JavaScript - ES5
JavaScript - ES5
JavaScript ES5, also known as ECMAScript 5, was released in 2009. It was the first major revision of JavaScript. It introduced many new features, includinggettersandsetters, and ‘use strict’ directive. ES5 has provided significant improvement over the previous versions of JavaScript. The new features introduced in ES5 make code more powerful, concise and easier to maintain.
New Added Features in JavaScript ES5
Here are the new methods, features, etc., added to the ES5 version of JavaScript.
- Array every()
- Array filter()
- Array forEach()
- Array isArray()
- Array indexOf()
- Array lastIndexOf()
- Array map()
- Array reduce()
- Array reduceRight()
- Array some()
- Date now()
- Date toJSON()
- Date toISOString()
- Function bind()
- JSON parse()
- JSON stringify()
- Multiline strings
- Object methods
- Object defineProperty()
- Property getters and setters
- Reserved words as property names
- ”use strict”
- String[number] access
- String trim()
- Trailing commas
Here, we have explained each feature in detail.
JavaScript Array every()
The array.every() method checks whether each element of the array follows a particular condition.
Example
In the below code, we use the array.every() method to check whether each element of the array is even.
Output
All elements of the array are even? trueJavaScript Array filter()
The array.filter() filters array elements and gets filtered elements in the new array.
Example
In the below code, we filter the array elements which are divisible by 10.
Output
Elements divisible by 10 are 10,20,30,40JavaScript Array forEach()
The array.forEach() traverses the array elements. It is similar to loops in JavaScript.
Example
The below code uses the array.forEach() method to traverse the array and print each array element in the new line.
Output
TATA
Honda
Toyota
MarutiJavaScript Array isArray()
The Array.isArray() method checks whether the object passed as an argument is an array.
Example
The below code checks whether the arr is an array using the Array.isArray() method.
Output
Is arr array? trueJavaScript Array indexOf()
The array.indexOf() method returns the first index of the particular element in the array.
Example
The below code finds the first index of 23 in the array, which is 0.
Output
The first index of 23 in the array is 0JavaScript Array lastIndexOf()
The array.lastIndexOf() method returns the last index of the particular element in the array.
Example
The code below finds the last index of the 45 in the array, which is 4.
Output
The last index of 45 in the array is 4JavaScript Array map()
The array.map() method returns a new array after mapping current array elements with new ones.
Example
The below code uses the map() method to create a new array by doubling the array elements.
Output
The new array is 2,4,6,8,10JavaScript Array reduce()
The array.reduce() method reduces the array to a single value.
Example
The below code multiples all elements of the array using the array.reduce() method.
Output
The multiplication result of the array elements is 16JavaScript Array reduceRight()
The array.reduceRight() method reduces the array from right to left instead of left to right.
Example
Output
The merged array elements in the reverse order is: doing?you are HowJavaScript Array some()
The array.some() method is used to check whether some elements of the array follow the particular condition.
Example
The code below checks whether the array contains 1 or more strings with lengths greater than 3.
Output
Array contains one or more strings with lengths greater than 3? trueJavaScript Date now()
The Date.now() method is used to get the total number of milliseconds since 1st January 1970.
Example
The below code finds the total milliseconds from 1st January 1970.
Output
Total milliseconds since 1st January, 1970 is: 1702540225136JavaScript Date toJSON()
The date.toJSON() method converts the date into the JSON date format. The JSON date format is the same as the ISO format. The ISO format is YYYY-MM-DDTHH:mm:ss.sssZ.
Example
The below code prints the current date in JSON format.
Output
The current date in JSON format is: 2023-12-18T06:12:52.366ZJavaScript Date toISOString()
The date.toISOString() method is used to convert the date into the ISO standard format.
Example
The below code prints the current date in the standard ISO string format.
Output
The current date in ISO string format is: 2023-12-18T06:13:50.159ZJavaScript Function bind()
The bind() method is used to borrow the method from another object.
Example
In the below code, the bird object contains the properties and printInfo() method. The animal object contains only name and category properties.
We used the bind() method to borrow the printInfo() method from the bird object for the animal object. In the output, the printInfo() method prints the information of the animal object.
Output
The name of the Animal is - LionJavaScript JSON parse()
The JSON.parse() method is used to convert the string into the JSON object.
Example
The code given below converts the string into an object. Also, we have printed the values of object properties in the output.
Output
The parsed JSON object values are -
name : Sam
age : 40
city: DelhiJavaScript JSON stringify()
The JSON.stringify() method converts the object into a string.
Example
The code below converts the obj object into the string using JSON.strringify() method.
Output
The object in the string format is - {"message":"Hello World!","messageType":"Welcome!"}JavaScript Multiline Strings
You can use the backslash () to break the line of the string and define the string in multiline.
Example
In the below example, we have defined the str string in multiple lines and used the backslash to break the line.
Output
The string is - Hi userJavaScript Object Methods
In ES5, object-related methods are added to manipulate and protect the objects.
Methods to Manipulate the Object
Sr.No.MethodDescription1create()To creat a new object, using an existing object as the prototype.2defineProperty()To make a clone of the object and add new properties to its prototype.3defineProperties()To define a property into a particular object and get the updated object.4getOwnPropertyDescriptor()To get the property descriptor for the properties of the object.5getOwnPropertyNames()To get object properties.6getPrototypeOf()To get the prototype of the object.7keys()To get all keys of the object in the array format.
Methods to Protect the Object
Sr.No.MethodDescription1freeze()To prevent adding or updating object properties by freezing the object.2seal()To seal the object.3isFrozen()To check if the object is frozen.4isSealed()To check if the object is sealed.5isExtensible()To check if an object is extensible.6keys()To get all keys of the object in the array format.7preventExtensions()To prevent the prototype updation of the object.
JavaScript Object defineProperty()
You can use the Object.definedProperty() method to define a single property of the object or update the property value and metadata.
Example
The code below contains the car object’s brand, model, and price properties. We used the defineProperty() method to define the gears property in the object.
Output
The obj object is - {"brand":"Tata","model":"Nexon","price":1000000,"gears":6}JavaScript Property getters and setters
The getters and setters are used to access and update the object properties securely.
Example
In the below code, the watch object contains the brand and dial properties. We have defined the getter using the get keyword to access the brand property value.
Also, we have defined the setter using the set keyword to set the dial property value in the upper case.
In this way, you can secure the updating object property value.
Output
The Watch brand is - Titan
The Watch dial is - SQUAREJavaScript Reserved words as property names
After ES5, you can use the reversed words as a property name in the object.
Example
In the below code, delete is used as an object property name.
Output
The obj object is - {"name":"Babo","delete":"Yes"}JavaScript “use strict”
The strict mode was also introduced in the ES5.
The strict mode allows you to overcome errors and write clear code. You can use the use strict directive to declare the strict mode.
Example
In the below code, you can uncomment the num = 43 line and observe the error. The strict mode doesnt allow developers to define the variables without using var, let, or const keywords.
Output
The value of num is: 43JavaScript String[number] access
Before ES5, the charAt() method was used to access the string character from a particular index.
After ES5, you can consider the string as an array of characters and access string characters from the specific index as you access array elements.
Example
Output
The first character in the string is: A
The third character in the string is: pJavaScript String trim()
The string.trim() method removes the whitespaces from both ends.
Example
In the below code, we used the string.trim() method to remove the whitespaces from the start and end of the str string.
Output
Hi, I am a string.JavaScript Trailing commas
You can add a trailing comma (A comma after the last element) in the array and object.
Example
In the below code, we have added the trailing comma after the last object property and array element. The code runs without error and prints the output.
Output
{"name":"John","profession":"Student"}
Hello,world!Note The JSON string doesnt allow the trailing comma.
For example,
let numbers = '{"num1": 10,"num2": 20,}';
JSON.parse(numbers); // Invalid
numbers ='{"num1": 10,"num2": 20}';
JSON.parse(numbers); // ValidIn ES5, mostly array and object-related methods are introduced.