Nullish Coalescing Operators in JavaScript


Nullish Coalescing Operator is a relatively new addition to JavaScript, introduced in ECMAScript 2020 (ES11). It provides a concise way to handle default values when dealing with null or undefined values.

The nullish coalescing operator is represented by ??, and it returns the right-hand operand when the left-hand operand is either null or undefined. Otherwise, it returns the left-hand operand.

Syntax

             const result = leftOperand ?? rightOperand;


Source Code

HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tutor Joes</title>
</head>
<body>
 
	<script src="js/script.js"></script>
</body>
</html>
To download raw file Click Here

script.js
//Nullish coalescing operator (??)

const a=null??'No Value';
console.log(a);

const b=null??45;
console.log(b);

//??=
const user={'name':'joes'};
console.log(user);
console.log(user.name);

user.city??='Salem';
console.log(user.city);
console.log(user);
To download raw file Click Here

List of Programs


JS Practical Questions & Answers


JS Practical Project