Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

The Nullish Coalescing Operator (??)

The ?? operator returns the first argument if it is not nullish (i.e., not null or undefined); otherwise, it returns the second argument.

Example

let name = null;
let text = “missing”;
let result = name ?? text;

The nullish operator has been supported in all browsers since March 2020.

comparison

The Optional Chaining Operator (?.)

The ?. operator returns undefined if an object is null or undefined, rather than throwing an error.

Example

// Create an object:
const car = {type:“Fiat”, model:“500”, color:“white”};
// Ask for car name:
document.getElementById(“demo”).innerHTML = car?.name;

The optional chaining operator has been supported in all browsers since March 2020.

comparison 2