ECMAScript 5 is also known as ES5 and ECMAScript 2009.
1. Most Important Features of ES5.
- The “use strict” Directive
- String.trim()
- Array.isArray()
- Array.forEach()
- Array.map()
- Array.filter()
- Array.reduce()
- Array.every()
- Array.indexOf()
- Array.lastIndexOf()
- JSON.parse()
2. String.trim()
String.trim() removes whitespace from both sides of a string.
| var str = " Hello World! "; |
| console.log(str.trim()); |
To remove the whitespace in the middle of a string, we need to use ‘replace’ method with Regular Expression.
| var str = " Hello World! "; |
| console.log(str.replace(/\s/g, '')); |
\s
is the regex for “whitespace”, and g
is the “global” flag, meaning match ALL \s (whitespaces).
3. Array.isArray()
| var fruits = ["Banana", "Orange", "Apple", "Mango"]; |
| console.log(Array.isArray(fruits)); |
4. Array.forEach()
| var array1 = ['a', 'b', 'c']; |
| |
| array1.forEach(function(element) { |
| console.log(element); |
| }); |
| |
| |
| |
| |
5. Array.map()
| var nums = [1, 4, 9, 16]; |
| |
| |
| const map = nums.map(x => x * 2); |
| |
| console.log(map); |
| |
6. Array.filter()
| var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; |
| |
| const result = words.filter(word => word.length > 6); |
| |
| console.log(result); |
| |
7. Array.reduce()
The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
| const array1 = [1, 2, 3, 4]; |
| const reducer = (accumulator, currentValue) => accumulator + currentValue; |
| |
| |
| console.log(array1.reduce(reducer)); |
| |
| |
| |
| console.log(array1.reduce(reducer, 5)); |
| |
8. Array.every()
The every() method tests whether all elements in the array pass the test implemented by the provided function.
| var array1 = [1, 30, 39, 29, 10, 13]; |
| |
| function isBelowThreshold(currentValue) { |
| return currentValue < 40; |
| } |
| |
| console.log(array1.every(isBelowThreshold)); |
| |
9. Array.indexOf()
The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
| var beasts = ['ant', 'bison', 'camel', 'duck', 'bison']; |
| |
| console.log(beasts.indexOf('bison')); |
| |
| |
| |
| console.log(beasts.indexOf('bison', 2)); |
| |
| |
| console.log(beasts.indexOf('giraffe')); |
| |
10. Array.lastIndexOf()
The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
| var animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo']; |
| |
| console.log(animals.lastIndexOf('Dodo')); |
| |
| |
| console.log(animals.lastIndexOf('Tiger')); |
| |
11. JSON.parse()
JSON.parse() is used to convert the text into a JavaScript object.
| var str = '{ "name":"John", "age":30, "city":"New York"}'; |
| var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}'); |
| console.log(obj.name); |
| console.log(obj.age); |
| console.log(obj.city); |
12. Reference