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 .
What's the difference between Null and Undefined Javascript

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


null vs undefined
  1. The concrete difference between them is that null is explicit, while undefined is implicit.
  1. 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
  1. Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.
  1. 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.
  1. 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
  1. "undefined" is not a valid value in JSON (JavaScript Object Notation) whereas "null" is a valid value in JSON.
  1. 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.