Since unlike Java, in javascript we have two different things "undefined" and "null" which actually represent the absence of some value, therefore many beginners in javascript get confused. But we need to know that there are some interesting differences between the two.
Let us talk about "undefined" first and gradually understand the difference between the two.
There are two things about "undefined" that we should always remember -
Let us talk about "undefined" first and gradually understand the difference between the two.
There are two things about "undefined" that we should always remember -
1.)The value "undefined".
2.)The type "undefined".
2.)The type "undefined".
To understand undefined as "value" , take a look at the below code-
var iAmNotDefined;
console.log("the value of iAmNotDefined is :", iAmNotDefined);
console.log("the value of iAmNotDefined is :", iAmNotDefined);
When you run this code what does this gives you on the console?
It is showing that the value of the variable "iAmNotDefined" is "undefined".
To make it more clear, understand that the any variable which is not assigned any value will implicitly get the value as "undefined".
Now understand the type of the variable "iAmNotDefined"
To make it more clear, understand that the any variable which is not assigned any value will implicitly get the value as "undefined".
Now understand the type of the variable "iAmNotDefined"
var iAmNotDefined;
console.log("the typeof iAmNotDefined is :", typeof(iAmNotDefined));
console.log("the typeof iAmNotDefined is :", typeof(iAmNotDefined));
JavaScript has a global variable undefined whose value is "undefined" and typeof undefined is also "undefined".
Now understand how "undefined" is different from "null"
Unlike "undefined", "null" is actually an assigned value.
The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values.
Now understand how "undefined" is different from "null"
Unlike "undefined", "null" is actually an assigned value.
The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values.
That means undefined means it hasn't been assigned yet, and null means it has been assigned to be nothing.
When do we actually get "undefined" in javascript:
1.)A declared variable without assigning any value to it. The example is given above
2.)Implicit returns of functions due to missing return statements.
To understand this run the below code in the console.
1.)A declared variable without assigning any value to it. The example is given above
2.)Implicit returns of functions due to missing return statements.
To understand this run the below code in the console.
function test(){
var hello = "hello";
}
console.log("Test return value : ", test());
var hello = "hello";
}
console.log("Test return value : ", test());
When run in console we get this -
3.)Return statements that do not explicitly return anything.
function test(){
var hello = "hello";
return;
}
console.log("Test return value : ", test());
var hello = "hello";
return;
}
console.log("Test return value : ", test());
When run the code we get the below output on console -
4.) Lookups of nonexistent properties in an object
var person = {firstName :'John', lastName : 'Doe'};
console.log("firstName: ",person.firstName);
console.log("middleName:", person.middleName );
console.log("firstName: ",person.firstName);
console.log("middleName:", person.middleName );
When run the code we get the below output on console -
5.)Function parameters that have not passed.
function test(param1, param2){
console.log(param1);
console.log(param2);
}
test("hello");//call the function with single parameter.
console.log(param1);
console.log(param2);
}
test("hello");//call the function with single parameter.
When run the code we get the below output on console -
6.)Any expression in the form of void(expression)
console.log(void "test"); // outputs "undefined"
When run the code we get the below output on console -
I hope you understand the undefined better now.