Setting a default variable value to undefined in a function - JavaScript?


The task we are going to perform in this article is setting a default variable value to undefined in a function. Before we jump into the article, let’s understand what are functions in JavaScript.

One of the core components of JavaScript are functions. In JavaScript, a function is comparable to a procedure-a collection of statements that carry out an action or compute a value. However, in order for a process to be considered a function, it must accept an input and produce an output with an evident connection between the input and the result.

Let’s see how to set a default variable value to undefined in a function.

Using The OR (||) Operator

For a set of operands, the logical OR (||) (logical disjunction) operator is true if and only if one or more of its operands are true. It is frequently applied to Boolean values. When used with non-Boolean values, the || operator will return a non-Boolean value because it actually returns the value of one of the given operands.

Syntax

Following is the syntax for OR (||) operator -

expr1 || expr2

Example

In the following example, we are running the script using the OR operator to set the value to "undefined”.

<!DOCTYPE html>
<html>
   <body style="background-color:#D5F5E3">
      <button onclick=getvalue()>Click For Value</button>
      <script>
         function getvalue(){
            let a=undefined;
            let b = 22;
            a || (a = b)
            document.write("The Value of a is:" +a)
         }
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of a button on the webpage. If the user clicks the button, the event gets triggered and displays the value of x.

Using the Nullish coalescing operator (??)

The logical operator known as the nullish coalescing (??) operator returns its left side operand unless its right side operand is null or undefined, in which case it returns its right side operand.

Syntax

Following is the syntax for nullish coalescing(??) -

leftExpr ?? rightExpr

Example

Considering the following example, where we are running the script using the nullish coalescing operator to set the value to "undefined".

<!DOCTYPE html>
<html>
   <body>
      <script>
         let a="Benz";
         let b = undefined;
         let c = "Audi";
         a ??= c;
         b ??= c;
         document.write("The vale of a:"+a+"<br>")
         document.write("The vale of b:"+b)
      </script>
   </body>
</html>

On running the above script, the output window will pop up, displaying the values on the webpage. We can observe and find the value for "undefined" displayed.

Example

Execute the below script and observe how we are using the nullish coalescing operator to set the value to "undefined."

<!DOCTYPE html>
<html>
   <body style="background-color:#E8DAEF">
      <button onclick=undefinedvalue()>Click For Values</button>
      <script>
         function undefinedvalue(){
            let array = ["RX100", 45, undefined, "Vespa", undefined]
            array.forEach((item, index)=>{
               array[index] ??= "TutorialsPoint"
            })
            document.write(JSON.stringify(array))
         }
      </script>
   </body>
</html>

When the script gets executed, it will generate an output consisting of a click button on the webpage. When the user clicks the button, the event gets triggered and displays an array along with the values for the undefined displayed.

Updated on: 21-Apr-2023

708 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements