Assign value if not exist in TypeScript

Find this useful? Support us: Star on GitHub 6
Category: Other | Language: TypeScript

In TypeScript, you can assign a value to a variable if it does not already exist using the nullish coalescing operator ??.

The nullish coalescing operator checks if the left-hand side operand is null or undefined. If so, it returns the right-hand side operand. Otherwise, it returns the left-hand side operand.

Here's an example:

let myVariable: string | undefined;

const myValue = myVariable ?? 'myDefaultValue';

console.log(myValue); // output: "myDefaultValue"

In this example, myVariable is not assigned any value. We use the ?? operator to assign a default value of 'myDefaultValue'. Since myVariable is undefined, the value of myValue will be 'myDefaultValue'.

Another example:

let myVariable: string | null = null;

const myValue = myVariable ?? 'myDefaultValue';

console.log(myValue); // output: null

In this example, myVariable is assigned the value null. However, since we are using the nullish coalescing operator, it will not use the default value 'myDefaultValue'. Instead, the value of myValue will be null.