It is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.

let’s see some use-cases that you might encounter frequently, where the ( ?? ) operator can make your life easier :)

(Code snippet—1.1)

The most common use-case is accessing a value from a nested Object in javascript.

In Code snippet—1.1 you can see that we are trying to access the key ‘name’ from the data object, which is a nested object, and it gives us the expected result.

(Code snippet—1.2)

In Code snippet—1.2 you can see that now the nested data object doesn’t contain the _‘name’ _key.And when we try to access the ‘name’ key from it, we get undefined.

Getting an undefined value is often not desirable. You might want to set a default value if the key that you are trying to access is sometimes undefined.

#es2020 #javascript #techniques #reactjs #web-development #tutorial #beginners-guide #working-example

Nullish Coalescing and Optional Chaining Operators
1.05 GEEK