JavaScript ES10: Whats New?

1. String.matchAll()

const string = "I am learning JavaScript not Java.";
const re = /Java[a-z]*/gi;

let result = string.matchAll(re);

for (match of result) {
console.log(match);
}
[
'JavaScript',
index: 14,
input: 'I am learning JavaScript not Java.',
groups: undefined
]
[
'Java',
index: 29,
input: 'I am learning JavaScript not Java.',
groups: undefined
]

2. Array.flat() & Array.flatMap()

const arr1 = [0, 1, 2, [3, 4]];console.log(arr1.flat());
// expected output: [0, 1, 2, 3, 4]
const arr2 = [0, 1, 2, [[[3, 4]]]];console.log(arr2.flat(2));
// expected output: [0, 1, 2, [3, 4]]

3. Object.fromEntries()

const entries = new Map([
['foo', 'bar'],
['baz', 42]
]);
const obj = Object.fromEntries(entries);console.log(obj);
// expected output: Object { foo: "bar", baz: 42 }

4. trimStart() & trimEnd()

trimStart
const greeting = ' Hello world! ';
console.log(greeting);
// expected output: " Hello world! ";
console.log(greeting.trimStart());
// expected output: "Hello world! ";
trimEndconst greeting = ' Hello world! ';console.log(greeting);
// expected output: " Hello world! ";
console.log(greeting.trimEnd());
// expected output: " Hello world!";

5. Symbol.Description

const symbol = Symbol("this is my description");
console.log(symbol.description) // this is my description

6. Optional Catch Binding

Before ES10
try{
//javascript line of code}
catch(error){
//code to handle exception
}
finally {
//clean up code
}
Optional Binding ES10
try {

} catch {

}

7. Function.toString()

function getMessage(msg) {
var name = msg;
// display name to console

console.log(`Hello ${name}`);
}
console.log(getMessage.toString());
Outputfunction getMessage(msg) {
var name = msg;
// display name to console

console.log(`Hello ${name}`);
}

8. Dynamic Imports can be assigned to a variable

element.addEventListener('click', async () => {
const module = await import(`./api-scripts/button-click.js`);
module.clickEvent();
});

9. BigInt datatype

typeof 20;
⇨ 'number'
typeof 20n;
⇨ 'bigint'
const limit = Number.MAX_SAFE_INTEGER;
⇨ 9007199254740991
const bigger = 9007199254740992n;
⇨ 9007199254740992n

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store