Null vs. Undefined in JavaScript | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (182).NET Core  (26).NET MAUI  (233)Angular  (115)ASP.NET  (49)ASP.NET Core  (80)ASP.NET MVC  (85)Azure  (42)Black Friday Deal  (1)Blazor  (243)BoldSign  (15)DocIO  (24)Essential JS 2  (108)Essential Studio  (198)File Formats  (73)Flutter  (140)JavaScript  (227)Microsoft  (121)PDF  (86)Python  (1)React  (107)Streamlit  (1)Succinctly series  (131)Syncfusion  (988)TypeScript  (32)Uno Platform  (3)UWP  (3)Vue  (45)Webinar  (54)Windows Forms  (59)WinUI  (72)Word  (18)WPF  (165)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (11)Business intelligence  (55)Button  (4)C#  (164)Chart  (154)Chart of the week  (62)Cloud  (16)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (74)Development  (696)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (47)Extensions  (31)File Manager  (8)Gantt  (23)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (530)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (48)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (426)Uncategorized  (68)Unix  (2)User interface  (67)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (650)What's new  (355)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Null vs. Undefined in JavaScript

Null vs. Undefined in JavaScript

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.

What is Null?

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);
Initial value of nullVar is null

Explore the best and most comprehensive JavaScript UI controls library in the market.

What is Undefined?

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:

  • To a declared but uninitialized variable.
    //A variable declared without a value assigned.
    var undefinedVar;
    console.log("The value of undefinedVar is ",undefinedVar);
    The value of undefinedVar is undefined.
  • The programmer declares the variable without initializing it to any value (not even null).
  • To a non-existent property of an object.
    //A non-existent property in an object.
    var undefinedObj={};
    console.log("The value of non-existent property 'name' is ",;
    The value of non-existent property 'name' is undefined.
  • To a return value of a function that’s not explicitly defined.
    //A return without an explicit value.
    var undefinedRet = function(){
    console.log("The return value of the function is ",undefinedRet());
    The return value of the function is undefined.
  • To formal arguments of a function without actual arguments.
    //Formal parameters of a function without actual arguments.
    var undefinedParam = function(arg1){
        console.log("The value of arg1 is ",arg1);
    The value of arg1 is undefined
  • To out-of-bound array elements.
    //Out of bound array elements are undefined.
    const array1 = [0,1,2,3];
    console.log("The 4th element in the array is ",array1[4]);
    The 4th element in the array is undefined.

Everything a developer needs to know to use JavaScript control in the web app is completely documented.

Similarities between Null and 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:

  • Both denote the absence of a value.
  • Both are primitive JavaScript values, meaning they are not considered objects and therefore don’t have methods or properties.
  • Both are false when encountered in a Boolean context (falsy values).
  • Despite both being falsy in a Boolean context, null or undefined is not loosely equal (==) to any other falsy value such as zero (0), an empty string (” “, ‘ ‘), or Not-a-Number (NaN). The only values null is loosely equal to are undefined and itself. This is due to the type-coercion quality of the loose equality operator of JavaScript.
    The following code snippet depicts this loose equality behavior between undefined and null.

    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!");
        console.log("null is not loosely equal to other falsy values!");
    The value in the variable is null.
    null is loosely equal to undefined!
    null is not loosely equal to other falsy values!

Differences between Null and Undefined

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:

  • JavaScript always assigns undefined to indicate the absence of a value by default, and it never automatically assigns null as a value. A variable will only return null if a programmer or an API specifically assigns the value null to the variable.
  • In a numerical context, undefined is evaluated to NaN while null is evaluated to 0.
    console.log(2+null); // 2
    console.log(2+undefined); // NaN
  • Even though null and undefined are loosely equal, they are not strictly equal.
    console.log( null === undefined) // false
  • Although both null and undefined are primitive values in JavaScript because of a historical error that transpired, typeof(null) returns “object”. On the other hand, typeof(undefined) is “undefined”.

Syncfusion JavaScript controls allow you to build powerful line-of-business applications.

How to choose between Null and Undefined

The following instances depict when and how you could utilize null and undefined in a meaningful manner based on the properties discussed so far:

  • Since undefined is the default value assigned by JavaScript to uninitialized variables, if you want to indicate the absence of a deal explicitly, always use null instead of undefined to avoid confusion.
  • To check if a variable has any value before proceeding further in a program, you can use the loose equality ==null to check for either null or undefined.For example, in the following program, the function assignVal() checks whether the num is undefined or null and assigns the value given by the user only if the variable num is not initialized to any value. As a result, the value in the variable c will not be changed by the function.
    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;
        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
  • If you try to check the absence of a value that has not been declared using the previous method, a ReferenceError will be thrown. In such instances, we can use the function typeof() to check whether a value has been declared and appropriately initialized using the following statement.
    typeof(undeclaredVar) !== "undefined" && 
    (typeof(undeclaredVar) !== "object" || !!undeclaredVar))

This statement will be evaluated as false if the variable undeclaredVar is:

  • Not declared.
  • Declared but not initialized (undefined).
  • Declared but initialized to null or undefined.

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 forumssupport portalor feedback portal. We are always happy to assist you!

Related blogs


Share this post:

Comments (1)

I fully agree with the views expressed in the article, but I still have a point of confusion: According to the article, `some_elem.getAttribute(‘nonexistent_prop’)` should return `undefined` (just like `some_elem.nonexistent_prop === undefined`), but in reality, it returns `null`…

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed