Nullish Coalescing Operator (??) in TypeScript Last Updated : 09 Jul, 2024 Comments Improve Suggest changes Like Article Like Report The nullish coalescing (??) operator is a logical operator in TypeScript that returns its right-hand side operand if the left-hand side operand is null or undefined; otherwise, it returns the left-hand side operand. This operator is useful for assigning default values to variables, ensuring more consistent and predictable code execution.SyntaxThe Nullish Coalescing Operator is represented by two adjacent question marks (?? ): variable ?? default_valueNote: At its core, the operator will return default_value when the variable is either null or undefined, alternatively it will return the actual variable. Example 1: Using Nullish Coalescing Operator in a FunctionIn the given below example we are demonstrating the use of the Nullish Coalescing Operator within a function. JavaScript function displayValue(input?: number) { const value = input ?? 100; console.log(value); } displayValue(); displayValue(50); Output:100 50Example 2: Setting Default Values in ObjectsIn the given below example the Nullish Coalescing Operator is used for setting default values for properties in objects, especially when dealing with JSON data. JavaScript interface Settings { volume?: number; brightness?: number; } const userSettings: Settings = { volume: 0 }; const volume = userSettings.volume ?? 50; const brightness = userSettings.brightness ?? 75; console.log(`Volume: ${volume}`); console.log(`Brightness: ${brightness}`); Output:Volume: 0 Brightness: 75Supported BrowsersThe browsers supported by TypeScript Nullish Coalescing Operator are listed below:Google Chrome 5.0Edge 12 Mozilla 4.0 Safari 5.0 Opera 11.1 Comment More infoAdvertise with us Next Article Nullish Coalescing Operator (??) in TypeScript pankajbind Follow Improve Article Tags : Web Technologies TypeScript TypeScript-Reference Similar Reads JavaScript Nullish Coalescing(??) Operator The nullish coalescing (??) operator 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. It's commonly used to provide default values for variables. Syntax: variable ?? default_valueBel 1 min read TypeScript Non-null Assertion Operator (Postfix !) Type TypeScript non-null assertion operator (!) is used to assert that a value is non-null and non-undefined, even when TypeScript's strict null checks are enabled. This operator tells TypeScript to treat the expression as if it's guaranteed to have a value, eliminating compile-time null and undefined ch 2 min read Optional Property Class in TypeScript TypeScript is an Object Oriented Programming language that allows you to create classes with optional properties which may or may not be assigned with a value. We will discuss two different ways of creating optional property classes in TypeScript: Table of Content By using the Question Mark(?)By ass 4 min read Nullish Coalescing Assignment (??=) Operator in JavaScript This is a new operator introduced by javascript. This operator is represented by x ??= y and it is called Logical nullish assignment operator. Only if the value of x is nullish then the value of y will be assigned to x that means if the value of x is null or undefined then the value of y will be ass 2 min read TypeScript in operator narrowing Type In this article, we will learn about the 'in' operator narrowing Type in Typescript. In TypeScript, the 'in' operator is used to narrow or refine the type of an object within a conditional statement or block. It checks whether a specific property or key exists within an object, and if it does, it na 3 min read TypeScript Optional Parameters in Callbacks In TypeScript, optional parameters in callbacks are parameters that may or may not be provided when the callback function is invoked. They are denoted by a ? after the parameter name, allowing for more flexible function calls without requiring all arguments.Syntaxtype MyCallback = (param1: string, p 2 min read How to declare nullable type in TypeScript ? In vanilla JavaScript, there are two primary data types: null and undefined. While TypeScript previously did not allow explicit naming of these types, you can now use them regardless of the type-checking mode. To assign undefined to any property, you need to turn off the --strictNullChecks flag. How 2 min read TypeScript Operators TypeScript operators are symbols or keywords that perform operations on one or more operands. Below are the different TypeScript Operators:Table of Content TypeScript Arithmetic operatorsTypeScript Logical operatorsTypeScript Relational operatorsTypeScript Bitwise operatorsTypeScript Assignment oper 6 min read Check if an Object is Empty in TypeScript In TypeScript, determining whether an object is empty involves checking if the object has any properties. This can be achieved through various built-in methods and loops. In this article, we will explore three different approaches to check if an object is empty in TypeScript.Table of ContentUsing Ob 2 min read How to enforce strict null checks in TypeScript ? In Typescript to enforce strict null checks in tsconfig.json file, we need to enable "strictNullChecks" to true. When "strictNullChecks" is false, the language generally ignores variables of type null and undefined. If null and undefined is used in places where a definite value is expected, it raise 2 min read Like