Skip to main content

Command Palette

Search for a command to run...

Busting "Undefined" JavaScript

Updated
•2 min read•View as Markdown
Busting  "Undefined" JavaScript

The general description of Undefined in JavaScript goes something like this👇

Undefined is a property of the global object, Which means it is a variable in the global scope.

So, Before going ahead on understanding the definition of Undefined in JavasScript. Let us first understand what it means to declare something in JavaScript.

When we write something with the keyword var then it is said to be a declaration.
For Example -

var name;

Now if we go further to the next step and assign some value to the declared variable this is called definition in JavaScript.
For Example -

name = Clark;

Now What is Undefined in JavaScript??
👉The declared variable which has no definition is said to be Undefined in JavaScript. which in simple terms would mean we have not assigned any value to the declared variable.

For Example -

var name ; // the result would be undefined

Now let us understand the working nature of Undefined in JavaScript.

Since JavaScript is a loosely typed language which means we are not correctly required to predict the kind of data that will be kept in a variable. Depending on the information you supply to a variable such as 👉' ' or " " to signify string values. JavaScript will automatically type it. But if we do not assign any value to a variable then a value of Undefined is automatically assigned to it by the JavaScript machine.

Let us understand through some syntax examples👇

Example 1:- var book; 
// here variable book is not defined so Js machine will assign undefined as its value.

Example 2:- var obj={}; 
// here object variable is not defined so the Js machine would assign a value of undefined.

Example 3:- var array =[]; 
// here array variable is not defined so the value of undefined is assigned.


Example 4:- var sunday=function(){
                         return;
                               }; 
// since the function variable Sunday is not returning any value so it is undefined.



Example 5:- var monday =function(a){
                                  return a;
                                 } 
                            monday();  
// Again undefined since function variable monday is trying to return an undefined value of a

Conclusion

In JavaScript, the declared variables are by default assigned a value of Undefined to it if it has no definition. The best way to check a variable or a function is undefined or not is by using the typeof operator.

References

Tutorials Point

MDN | Undefined