Loading...

Difference between ?? and || in Typescript or Javascript?

question typescript javascript
Ram Patra Published on July 17, 2024

In TypeScript (and JavaScript), the ?? (nullish coalescing operator) and || (logical OR operator) are both used to provide default values, but they behave differently in terms of the conditions under which they return the right-hand operand.

Nullish Coalescing Operator (??)

The ?? operator returns the right-hand operand when the left-hand operand is null or undefined. If the left-hand operand is any other value (including falsy values like 0, NaN, ''), it returns the left-hand operand.

Example:

const value = null ?? 'default';
console.log(value); // Output: 'default'

const value2 = undefined ?? 'default';
console.log(value2); // Output: 'default'

const value3 = 0 ?? 'default';
console.log(value3); // Output: 0

const value4 = '' ?? 'default';
console.log(value4); // Output: ''

const value5 = false ?? 'default';
console.log(value5); // Output: false

Logical OR Operator (||)

The || operator returns the right-hand operand when the left-hand operand is falsy. Falsy values include false, 0, -0, 0n, "", null, undefined, and NaN. If the left-hand operand is any truthy value, it returns the left-hand operand.

Example:

const value = null || 'default';
console.log(value); // Output: 'default'

const value2 = undefined || 'default';
console.log(value2); // Output: 'default'

const value3 = 0 || 'default';
console.log(value3); // Output: 'default'

const value4 = '' || 'default';
console.log(value4); // Output: 'default'

const value5 = false || 'default';
console.log(value5); // Output: 'default'

Key Differences

  • Falsy Values: || considers many values (like 0, '', false, NaN) to be falsy and returns the right-hand operand for these values. ??, on the other hand, only considers null and undefined to be nullish and returns the right-hand operand only for these values.
  • Use Case: Use ?? when you specifically want to handle null and undefined cases. Use || when you want to provide a default value for any falsy value.

Summary

  • Use ?? (nullish coalescing) when you want to provide a default value only if the left-hand side is null or undefined.
  • Use || (logical OR) when you want to provide a default value for any falsy value (including null, undefined, 0, false, '', NaN).

Choosing between ?? and || depends on the specific behavior you need for handling default values in your code.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on July 17, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question typescript react September 28, 2024 How to add a custom element to a Next.js/Typescript project?

Let’s say I have a custom element setapp-badge that I want to use in my tsx files. If I go ahead and use it, the compiler will throw an error and Next.js will fail to build. It seems the problem might be a combination of how Next.js, TypeScript, and custom elements work together. Therefore, let’s try an approach that avoids the namespace/module issues while ensuring custom elements are recognized in a Next.js/TypeScript project.

question typescript javascript August 11, 2024 What is module inside compilerOptions in tsconfig.json and what to use?

The module option inside compilerOptions in the tsconfig.json file of a TypeScript project specifies the module code generation system that the TypeScript compiler should use when emitting JavaScript. This option determines how the TypeScript code will be transformed into JavaScript modules, affecting how modules are loaded, interpreted, and linked in the resulting JavaScript code.

question front-end javascript August 28, 2024 8 best Javascript libraries for building fast-changing tables

For handling fast-changing tables in JavaScript, you’ll want libraries that are optimized for performance, support real-time data updates, and are flexible enough to handle a wide range of use cases. Here are some of the best libraries: