JS 2019 – ECMAScript 2019
Last Updated :
24 Aug, 2023
ECMAScript 2019, also known as ES10, introduced features like Array.flat(), Array.flatMap(), Object.fromEntries(), and Symbol. description, and some string methods, for enhancing JavaScript’s capabilities and expressiveness.
JavaScript 2019 (ES10) or ECMAScript 2019 new features are:
Name
|
Description
|
String.trimStart() |
trimming leading whitespace characters from a string.
|
String.trimEnd() |
removes trailing whitespace characters from the end of a string
|
Object.fromEntries |
Creates an object from a key-value pairs array.
|
Optional catch binding |
omitting the catch parameter in try-catch blocks, simplifying error handling
|
Array.flat() |
flattening nested arrays to a single-dimensional array.
|
Array.flatMap() |
mapping and flattening nested arrays simultaneously for streamlined operations.
|
Revised Array.Sort() |
Equal value elements retain relative positions during sorting.
|
Revised JSON.stringify() |
safely handles UTF-8 code points (U+D800 to U+DFFF) accurate compatibility with JSON.parse().
|
Separator symbols allowed in string litterals |
separator symbols are allowed within numeric and string literals to improve readability without affecting their values
|
Revised Function.toString() |
converts a function to a string representation
|
We will explore all the above methods along with their basic implementation with the help of examples.
This method is used to trims leading whitespace characters from the beginning of a string, enhancing JavaScript’s string manipulation capabilities for improved data handling and formatting.
Syntax:
string.trimStart();
Example: Here the basic example of String.trimStart().
Javascript
let text = " GeeksforGeeks" ;
let result = text.trimStart();
console.log(result);
|
This method is used to removes trailing whitespace characters from the end of a string, offering enhanced string manipulation capabilities for improved data handling and formatting in JavaScript.
Syntax:
string.trimEnd()
Example: Here is the basic example String.trimEnd().
Javascript
let text = "GeeksforGeeks " ;
let result = text.trimEnd();
console.log(result);
|
Object.fromEntries() converts an array of key-value pairs into an object, simplifying object creation from structured data in JavaScript.
Syntax:
Object.fromEntries( iterable );
Example: In this example we are using the above-explained method.
Javascript
const data = [
[ 'name' , 'Nikita' ],
[ 'age' , 21],
[ 'city' , 'Noida' ]
];
let result = Object.fromEntries(data);
console.log(result);
|
Output
{ name: 'Nikita', age: 21, city: 'Noida' }
Optional catch binding in ECMAScript 2019 allows omitting the catch parameter in try-catch blocks, simplifying error handling when the caught error object isn’t needed.
Syntax:
try {
// Code that may throw an error
} catch (error) {
// Code that may throw an error
}
Example: Here is the basic example of using try and catch method.
Javascript
function geekFunc() {
let a = 10;
try {
console.log( "Value of variable a is : " + a);
}
catch (e) {
console.log( "Error: " + e.description);
}
}
geekFunc();
|
Output
Value of variable a is : 10
Array.flat() method collapses nested arrays within an array to a single-dimensional array, simplifying array manipulation and enhancing data processing in JavaScript.
Syntax:
arr.flat([depth])
Example: In this example we are using the above-explained method.
Javascript
let numbers = [[ '1' , '2' ], [ '3' , '4' ,
[ '5' , [ '6' ], '7' ]]];
const flatNumbers = numbers.flat(Infinity);
console.log(flatNumbers);
|
Output
[
'1', '2', '3',
'4', '5', '6',
'7'
]
Array.flatMap() combines mapping and flattening of nested arrays within an array, simplifying data transformation and manipulation by applying a function and then flattening the result.
Syntax:
let A = array.flatMap(function callback(current_value, index, Array)) {
// It returns the new array's elements.
}
Example: In this example, the flatMap() method applies a function to each element of the num1 array, doubling and squaring each number, and then flattens the resulting arrays into a single-dimensional array.
Javascript
const num1 = [1, 2, 3, 4];
const result =
num1.flatMap(number => [number * 2, number ** 2]);
console.log(result);
|
Output
[
2, 1, 4, 4,
6, 9, 8, 16
]
Method 7: Revised Array.Sort() Method
After ES2019, Array.sort() maintains relative positions of elements with equal values, ensuring stability in sorting algorithms for consistent results when sorting elements based on a value.
Syntax:
arr.sort(compareFunction)
Example:
Javascript
const students = [
{ name: 'Anil' , score: 85 },
{ name: 'Balram' , score: 90 },
{ name: 'Chinu' , score: 85 },
{ name: 'Dinesh' , score: 78 },
];
students.sort((a, b) => a.score - b.score);
console.log(students);
|
Output
[
{ name: 'Dinesh', score: 78 },
{ name: 'Anil', score: 85 },
{ name: 'Chinu', score: 85 },
{ name: 'Balram', score: 90 }
]
Before ES2019, JSON.stringify() broke UTF-8 code points (U+D800 to U+DFFF). Post-revision, it them, ensuring accurate conversion and compatibility with JSON.parse().
Syntax:
let text = JSON.stringify("\u26D4");
Example: In this example we are using the above-explained method.
Javascript
let jsonStr1 = JSON.stringify( 'Hello\uD83D\uDE00Geeks' );
let result = JSON.parse(jsonStr1);
console.log(result);
|
Method 9: Separator Symbols Allowed in String Litterals
In JavaScript ES2019, separator symbols (underscores) are allowed within numeric and string literals to improve readability without affecting their values or behavior.
Syntax:
let num1 = 1_000_000_000;
let str1 = 'Hello_World';
let text1 = "\u2028";
Example: In this example we are using the above mentioned methods.
Javascript
let num1 = 1_000_000_000;
let str1 = 'Hello_World' ;
let text1 = "\u2028" ;
console.log(num1);
console.log(str1);
console.log(text1);
|
Output
1000000000
Hello_World
Method 10: Revised Function.toString() Method
Function.toString() converts a function to a string representation, revealing its source code, including comments and formatting, facilitating debugging and dynamic code generation in JavaScript.
Syntax:
let result = myFunction.toString();
Example: Here the basic example of above mentioned method.
Javascript
function myFunction(name) {
return `Hello, Geeks!`;
}
let result = myFunction.toString();
console.log(result);
|
Output
function myFunction(name) {
return `Hello, Geeks!`;
}
Supported browser:
- Chrome 1
- Edge 12
- Firefox 1
- Safari 1
- Opera 3
Share your thoughts in the comments
Please Login to comment...