How to Set a Variable’s Value if it’s Undefined in JavaScript

How to Set a Variable’s Value if it’s Undefined in JavaScript

In this article, I will show you how to set a variable’s value if it’s undefined in Javascript. You can use the if/else statement, ternary operator or logical nullish assignment. Let’s follow the methods below.

Set a Variable’s Value if it’s Undefined in JavaScript

Data type undefined is a special data type in JavaScript that consists of only one undefined value, for example:

let name = undefined;
console.log(name);

Output

undefined

If the variable’s value is undefined, you can use the method below to set a new value for it.

Using if/else statement

We use the strict equality operator (===) to check whether the variable’s value is equal to undefined. If it returns true, then we set the value for that variable. 

let name1;
let name2 = "Lisa";
 
function myFunction(obj){
    if(obj === undefined){
        obj = "Jade";
    } else {
        obj = obj;
    }
    console.log(obj);
}
 
myFunction(name1);
myFunction(name2);

Output

Jade
Lisa

This method is easy to understand but quite long. Instead of using the if/else statement, we have a more straightforward and concise way below.

Conditional (ternary) operator

The basic writing and usage of the ternary operator are the same as the if statement.

The conditional operator in JavaScript is an operator that is composed of three arguments, including the conditional expression; the result when the condition is true; and the result when the condition is false. The result can be a value returned or processing performed later, depending on whether the specified condition is true or false.

The ternary operator in JavaScript allows us to test a specific condition in a single line of code and return a value instead of using multiple if-else statements, making the code simple and intelligent.

Syntax:

condition ? true_value : false_value;

Parameters

  • condition: is the conditional expression.
  • true_value: is the return value when the condition is true.
  • false_value: is the return value when the condition is false.

Code sample:

let name1;
let name2 = "Lisa";
 
function myFunction(obj){
    obj= (obj==undefined) ? "Rose": obj;
    console.log(obj);
}
 
myFunction(name1);
myFunction(name2);

Output

Rose
Lisa

Logical nullish assignment (??=)

The logical nullish assignment (x ??= y) operator only assigns if x is nullist (null or undefined).

Code sample

let name1;
let name2 = "Lisa";

function myFunction(obj){
    obj ??= "Misa";
    console.log(obj);
}
 
myFunction(name1);
myFunction(name2);

Output

Misa
Lisa

Summary

Above, I showed you how to set a variable’s value if it’s undefined in Javascript . The logical nullification assignment(??=) is probably the most convenient one you should use. To better understand the lesson’s content, practice rewriting today’s examples. And let’s learn more about Javascript in the next lessons here. Have a great day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *