Home >Web Front-end >JS Tutorial >No need of Try-Catch!! Javascript introduced new safe assignment operator✌

No need of Try-Catch!! Javascript introduced new safe assignment operator✌

WBOY
WBOYOriginal
2024-08-25 15:00:36552browse

No need of Try-Catch!! Javascript introduced new safe assignment operator✌

If you are a javascript developer then you definitely know about try-catch block which is used to deal with errors. But now you can use new Safe Assignment operator proposal (?=)
try-catch blocks lead to nested code making it harder to read and maintain.
?= operator reduces nesting and it gives result of function into tuple.
If an error occurs, it returns [error,null]otherwise it returns [null, result].

Example-

async function fetchData() {
     const [error, response] ?= await fetch("https://api.example.com/data");
     if (error) return handleError(error);
     return response;
   }

The above is the detailed content of No need of Try-Catch!! Javascript introduced new safe assignment operator✌. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn