Null Vs. Undefined in JavaScript
The value 'undefined' denotes that a variable has been declared, but hasn't been assigned any value. On the other hand, 'null' refers to a non-existent object, which basically means 'empty' or 'nothing'. They're both values usually used to indicate the absence of something . Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object .
Null
Null is merely a conceptual object meant to represent nothingness . The value null represents the intentional absence of any object value. Unfortunately, in JavaScript, the data type of null is an object . You can empty an object by setting it to null :
var arr = [ 'a', 'b', 'c' ];
alert(arr);
//a,b,c
arr = null;
alert(arr);
//null
alert(typeof arr);
//object
Here we can see, first arr assigned some values and later assigned to null . Now value is null, but type is still an object.
When defining a variable that is meant to later hold an object , it is advisable to initialize the variable to null as opposed to anything else. That way, you can explicitly check for the value null to determine if the variable has been filled with an object reference at a later time.
Undefined
In JavaScript, "undefined" is a global variable (a property of the global object), that is created at run time. Undefined means a variable has been declared, but the value of that variable has not yet been defined . Also, when you do not supply an argument for a function parameter, it will have the value of "undefined" . Furthermore, when a function does not return a value, it returns "undefined". It is one of JavaScript's primitive types .
var testVal;
alert(testVal);
//undefined
console.log(typeof testVal);
alert(testVal);
//undefined
testVal = undefined;
alert(testVal);
//undefined
If you try to use a variable that doesn't exist and has not been declared, then JavaScript will throw an error var name is not defined and script will stop executing. However, if you use typeof undeclared_variable , then it will return undefined.
Null Vs. Undefined

- The concrete difference between them is that null is explicit, while undefined is implicit.
- In JavaScript, both null and undefined have a falsy value so when you do a equality check with == they are considered the same. If you use a strict equality check with === they are considered different.
alert(null === null);
//true;
alert(undefined === undefined);
//true;
alert(undefined === null);
//false
alert(undefined == null);
//true
- Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.
- You can change the value of undefined by undefined=true; but you cannot change the value or null. This means that, null is object type where as undefined is undefined type.
- While performing primitive operations, null is converted to zero (0) whereas undefined is converted to NaN.
var tUndefined;
alert(tUndefined + 100);
//NaN
var tNull = null;
alert(tNull + 100);
//100
- "undefined" is not a valid value in JSON (JavaScript Object Notation) whereas "null" is a valid value in JSON.
- You might consider undefined to represent a system-level, unexpected, or error-like absence of value and null to represent program-level, normal, or expected absence of value.
Related Topics
- JavaScript Interview Questions (Part2)
- JavaScript Interview Questions (Part3)
- Is JavaScript a true OOP language?
- Advantages and Disadvantages of JavaScript
- Difference Between JavaScript and ECMAScript?
- What is noscript tag?
- Escaping Special Characters in JavaScript
- What is undefined x 1 in JavaScript?
- JavaScript : Logical Operators
- Difference between '=', '==' and '===' operators?
- How to reload a page using JavaScript?
- How to write html code dynamically using JavaScript?
- How to add html elements dynamically with JavaScript?
- How to load another html page from javascript?
- What Is The Disadvantages Using InnerHTML In JavaScript?
- What is Browser Object Model
- How to detect the OS on the client machine in JavaScript?
- Difference between window, document, and screen in Javascript?
- Difference between the substr() and substring() in JavaScript?
- How to replace all occurrences of a string in JavaScript?
- How to test a string as a literal and as an object ?
- What is Associative Array? How do we use it?
- What is an anonymous function in JavaScript?
- What is the use of 'bind' method in JavaScript?
- Pure functions Vs. Impure functions in javascript
- Is Javascript a Functional Programming Language?
- What's the Difference Between Class and Prototypal Inheritance?
- Javascript, Pass by Value or Pass by Reference?
- How to prevent modification of an object in Javascript?
- What is 'this' keyword in JavaScript?
- How Does Function Hoisting Work in JavaScript?
- What do mean by NULL in Javascript?
- What does the delete operator do in JavaScript?
- What is the Infinity property used for in Javascript?
- Event bubbling and Event Capturing in JavScript?
- What is "strict mode" and how is it used in JavaScript?
- What is the difference between .call() and .apply()?
- Entire content of a JavaScript source file in a function block?
- What is an immediately-invoked function expression?
- What is escape & unescape String functions in JavaScript?
- What is the instanceof operator in JavaScript?
- What Are RESTful (REpresentational State Transfer)Web Services?
- What is Unobtrusive JavaScript & Why it's Important?
- What Does JavaScript Void(0) Mean?
- What are JavaScript Cookies?
- Difference between Client side JavaScript and Server side JavaScript
- TypeError: document.getelementbyid(...) is null
- Uncaught TypeError: Cannot read property of undefined In JavaScript