How To Set A Variable’s Value If It’s Null In JavaScript

To set a variable’s value if it’s Null in JavaScript, I will use some operators in Javascript to do that. Check out this article of mine to learn about other uses of some of these operators.

How to set a variable’s value if it’s Null in JavaScript?

Use Conditional (Ternary) Operator  

Conditional (Ternary) Operators The Conditional (Ternary) Operator can be used to set a variable’s value if it’s Null quite quickly and neatly.

Syntax:

(condition) ? x : y

Parameters

  • x: is the statement used when your conditional is true.
  • y: is the statement used when your conditional is false.

Example:

// Create function Set a Variable's Value if it's Null 
function setValueIfNull(variable){
    let result = "variable's value is " ;
    variable === null ? result =  result + "null" : result =  result + variable;
    return result;
}

// Test function
console.log(setValueIfNull("LearnShareIT"));
console.log(setValueIfNull(null));

Output:

variable's value is LearnShareIT
variable's value is null

In the function setValueIfNull, I checked if the value of the variable passed in was null or not. If so, the function will return a string stating its value is null. Otherwise, the function will report its value if the test result is null. Check yes.

We can also use the if conditional to set a variable’s value if it’s Null, which would look like this:

// Create function Set a Variable's Value if it's Null 
function setValueIfNull(variable){
    let result = "variable's value is " ;
    if(variable === null)
     return result + "null";
    else
     return result + variable;
}

// Test function
console.log(setValueIfNull("LearnShareIT"));
console.log(setValueIfNull(null));

Output:

variable's value is LearnShareIT
variable's value is null

Conditional (Ternary) Operator is similar to if we can use it, we can replace it with Conditional (Ternary) Operator or vice versa.

Use the nullish coalescing operator (??)

Syntax:

leftExpr ?? rightExpr

This operator will help us check if the value of a variable is null or undefined. If so, we will get the value on the operator’s left. If so, then get the other side to understand the above operator. Please visit MDN docs.

Example:

// Create a variable to test
var nullValue = null;
var strValue = "LearnShareIT";

// Test value
var result = nullValue ?? "null";
var result2 = strValue ?? "null";

console.log("variable's value is " + result);
console.log("variable's value is " + result2);

Output:

variable's value is null
variable's value is LearnShareIT

We see that with a variable with a null value, the operator (??) will change its value to a string ‘null’, and for a variable with a non-null value, it will not change it and keep the value. 

Summary

I showed you two ways to set a variable’s value if it’s Null in JavaScript. To master using the above methods, you should follow me when you see them again in the future. It will help you remember and understand better. I hope this article helps you and your program. Good luck.

Leave a Reply

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