JavaScript 2022 - New Features - Part 1

Photo by Jess Bailey on Unsplash

JavaScript 2022 - New Features - Part 1

ECMAScript 2022 (ES13) - New Capabilities

Using at() function

The at() function is used for extracting a value at a given index in an array. The index can be given as negative or positive value.

// at method 
let arr= [1,2,3,4];
console.log(arr.at(1), arr.at(-1), arr.at(-2)); // 2,4,3
let arr2 = ["ram", "hari","gopi","suresh"];
console.log(arr2.at(-1),arr2.at(-2), arr2.at(2)); // suresh, gopi, gopi

In the above code the at() function is used for extracting values from an array by supplying index.

Using hasOwn() function

The hasOwn() is a static function on the Object. It returns true if a given property belongs to an object.

//Object.hasOwn() -If the given property belongs to an object
let emp = {
    no:100,
    name:"ram",
    salary:1000
};

console.log(Object.hasOwn(emp,'no'), Object.hasOwn(emp, 'name'), Object.hasOwn(emp,'salary')); // true true true
console.log(Object.hasOwn(emp,'dept')); // false

In the above code hasOwn() function returns false when emp object is checked for dept property.

Error cause property

It helps in tracing the origin of error with details.The cause property is created on an error object which later can be examined on occurrence of error.

function connectToDatabase(usn,pwd) {
    let connectionError = new Error("Invalid USN/PWD");
    let str = 'usn:' + usn +' pwd:' + pwd;
    connectionError.cause = new Error("details are:"+str);
    throw connectionError;
}

function getDataFromDatabase() {
    try {
        connectToDatabase("ram","javascript");        
    }catch (error) {
        console.error("Error:", error.message);
        if (error.cause) {
            console.error("Caused by:", error.cause);
        }
    }
}

getDataFromDatabase();

In the above code user tries to connect to a DB by providing username and password. An error object is raised when the connectToDatabase() function is called.In the connectToDatabase() function cause property is initialized. The cause property is checked in the catch clause and desired action can be taken.

Using cause property the internal error details can be initialized and extracted later.This is useful when the control execution is nested.

Complete Code Listing

// at method 
let arr= [1,2,3,4];
console.log(arr.at(1), arr.at(-1), arr.at(-2)); // 2,4,3
let arr2 = ["ram", "hari","gopi","suresh"];
console.log(arr2.at(-1),arr2.at(-2), arr2.at(2)); // suresh, gopi, gopi

//Object.hasOwn() -If the given property belongs to an object
let emp = {
    no:100,
    name:"ram",
    salary:1000
};

console.log(Object.hasOwn(emp,'no'), Object.hasOwn(emp, 'name'), Object.hasOwn(emp,'salary')); // true true true
console.log(Object.hasOwn(emp,'dept')); // false

function connectToDatabase(usn,pwd) {
    let connectionError = new Error("Invalid USN/PWD");
    let str = 'usn:' + usn +' pwd:' + pwd;
    connectionError.cause = new Error("details are:"+str);
    throw connectionError;
}

function getDataFromDatabase() {
    try {
        connectToDatabase("ram","javascript");        
    }catch (error) {
        console.error("Error:", error.message);
        if (error.cause) {
            console.error("Caused by:", error.cause);
        }
    }
}

getDataFromDatabase();
console.log("Completed");

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!