ECMAScript 2023(ES14) : New Capabilities
Using findLast() function
The findLast() function returns last value that matches the condition.It takes a predicate which identifies the strategy for the condition.
// findlast - last value that matches the condition
// does not modify the array
let arr = [11,29,63,90,29,74];
let v = arr.findLast(arg => arg > 70);
console.log("last value", v); // 74
v = arr.findLast(arg => {
return arg < 30 ;
});
console.log("last value", v); // 29
// iteration is from last to first
v = arr.findLast ((arg,idx) => {
console.log(idx, arg);
});
In above code findLast() function returns the last value that matches the condition.
The findLast() function iterates from last to first value in the array.
In the below code findLast() function is used with an array which contains objects.
// findLast - above different result
const users = [
{ id: 1, name: 'Blic' },
{ id: 2, name: 'Balram' },
{ id: 3, name: 'Baldev' },
];
const lastUser = users.findLast((user) => user.name.startsWith('B'));
console.log(lastUser); // Output: { id: 3, name: 'Baldev' }
Using findLastIndex function
The findLastIndex() function is used for finding the index of the last element that matches the condition
//findLastIndex
let arr = [11,29,63,90,29,74];
let idx = arr.findLastIndex(arg => arg>9);
console.log("index", idx); // 5
Using toSpliced function
The toSpliced() function returns a changed array by keeping the host array intact.
The toSpliced() function can be used for
Adding elements into the array
Updating elements of the array
Deleting elements from array
The below code shows how to remove elements from array
// toSpliced method - remove elements
let scores = [11, 22, 33, 41, 95];
let arr = scores.toSpliced(0, 2);
// 0- starting index, 2- number of elements to be removed
console.log("Spliced array", arr); // New array with elements [33, 41, 95]
The below code shows how to insert elements to array
arr = scores.toSpliced(0, 0, 1, 2);
// 0-start index, 0- no removal, 1 & 2 - elements to be inserted
console.log(arr); // New array with elements [1,2,11,22,33,41,95]
Hashbang support
In es14 the programmer can use Hashbang in code to execute the program directly.Hashbang has to be the first instruction. The syntax is listed below
#!/usr/bin/env node
console.log("Hashbang Supported in JavaScript");
Change the mode of the program to execute if there is a permission error.
Command : chmod +x program.js
Execute: ./program.js
Symbol as Weakmap key
From es14 Symbol can be used as a key in Weakmap collection
// symbol as key
const ky = Symbol('mykey');
const wkmap = new WeakMap();
wkmap.set(ky, 'value in weakmap');
console.log(wkmap.get(ky)); // Output: 'This is a secret message.'
Complete Code Listing
#!/usr/bin/env node
console.log("Hashbang Supported in JavaScript");
// findlast - last value that matches the condition
// does not modify the array
let arr = [11,29,63,90,29,74];
let v = arr.findLast(arg => arg > 70);
console.log("last value", v); // 74
v = arr.findLast(arg => {
return arg < 30 ;
});
console.log("last value", v); // 29
// iteration is from last to first
v = arr.findLast ((arg,idx) => {
console.log(idx, arg);
});
// findLast - above different result
const users = [
{ id: 1, name: 'Blic' },
{ id: 2, name: 'Balram' },
{ id: 3, name: 'Baldev' },
];
const lastUser = users.findLast((user) => user.name.startsWith('B'));
console.log(lastUser); // Output: { id: 3, name: 'Baldev' }
//findLastIndex
arr = [11,29,63,90,29,74];
let idx = arr.findLastIndex(arg => arg>9);
console.log("index", idx); // 5
// toSpliced method - remove elements
let scores = [11, 22, 33, 41, 95];
arr = scores.toSpliced(0, 2);
// 0- starting index, 2- number of elements to be removed
console.log("Spliced array", arr); // New array with elements [33, 41, 95]
arr = scores.toSpliced(0, 0, 1, 2);
// 0-start index, 0- no removal, 1 & 2 - elements to be inserted
console.log(arr); // New array with elements [1,2,11,22,33,41,95]
// symbol as key
const ky = Symbol('mykey');
const wkmap = new WeakMap();
wkmap.set(ky, 'value in weakmap');
console.log(wkmap.get(ky)); // Output: 'This is a secret message.'