In JavaScript, null and undefined data types may appear similar when viewed at an abstract level. As a result, developers often need clarification and help when debugging errors related to these two values.
This article will go through the definitions of null and undefined along with their similarities, fundamental differences, and how you could utilize each of these values in your program.
The primitive type null indicates the intentional absence of a value. Therefore, in JavaScript, programmers often assign null to a variable to denote that the variable is declared and initialized and currently doesn’t hold a value. Still, a value can be expected in the future.
The following code snippet shows how to assign null data type to a variable.
var nullVar = null; console.log("Initial value of nullVar is ", nullVar); /*Result: Initial value of nullVar is null */
undefined is a primitive value automatically assigned by JavaScript to indicate the unintentional absence of any object value.
Some of the situations where JavaScript assigns the value undefined are:
//A variable declared without a value assigned. var undefinedVar; console.log("The value of undefinedVar is ",undefinedVar); /* Result: The value of undefinedVar is undefined. */
//A non-existent property in an object. var undefinedObj={}; console.log("The value of non-existent property 'name' is ", undefinedObj.name); /* Result: The value of non-existent property 'name' is undefined. */
//A return without an explicit value. var undefinedRet = function(){ return; }; console.log("The return value of the function is ",undefinedRet()); /* Result: The return value of the function is undefined. */
//Formal parameters of a function without actual arguments. var undefinedParam = function(arg1){ console.log("The value of arg1 is ",arg1); }; undefinedParam(); /* Result: The value of arg1 is undefined */
//Out of bound array elements are undefined. const array1 = [0,1,2,3]; console.log("The 4th element in the array is ",array1[4]); /* Result: The 4th element in the array is undefined. */
Even though there are fundamental differences between null and undefined (which will be discussed in the next section in detail), programmers often tend to use them interchangeably due to the following similarities:
var nullVar = null; console.log("The value in variable is ", nullVar); if (nullVar == undefined){ console.log("null is loosely equal to undefined!"); }; if(nullVar == 0 || nullVar == NaN || nullVar == ''){ console.log("null is loosely equal to other falsy values!"); } else{ console.log("null is not loosely equal to other falsy values!"); }; /*Result: The value in the variable is null. null is loosely equal to undefined! null is not loosely equal to other falsy values! */
Despite the similarities between null and undefined, they may appear or be used in different places in a program due to their differences.
Some of the significant differences between null and undefined are:
console.log(2+null); // 2 console.log(2+undefined); // NaN
console.log( null === undefined) // false
The following instances depict when and how you could utilize null and undefined in a meaningful manner based on the properties discussed so far:
let assignVal = (num,val) => { //if num is either null or undefined, assign the new value. // otherwise, keep the same value. if( num == null){ num = val; } else{ num = num; }; return num; }; var a; var b = null; var c = 0; a = assignVal(a,100); b = assignVal(b,50); c = assignVal(c,80); console.log("a =", a,",b =",b,",c=",c); /* Result: a = 100 ,b = 50 ,c= 0 */
typeof(undeclaredVar) !== "undefined" && (typeof(undeclaredVar) !== "object" || !!undeclaredVar))
This statement will be evaluated as false if the variable undeclaredVar is:
As a result, you can safely conclude that a variable is properly declared and has a valid value when the previous statement is evaluated as true.
This article covered the basics of null and undefined values and some best practices to check and handle errors in JavaScript apps that may arise from them.
Since runtime errors in JavaScript apps often occur when variables that are undefined or null are mishandled, it’s crucial for a developer to understand and differentiate between the two specific primitive values clearly. I hope this article was a helpful guide in understanding the difference between null and undefined.
Thank you for reading!
Syncfusion’s Essential JS 2 is the only suite you will need to build an app. It contains over 65 high-performance, lightweight, modular, and responsive UI components in a single package. Download a free trial to evaluate the controls today.
If you have any questions or comments, you can contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!