Introduction
Welcome to the "Advanced JavaScript" article series. In this series, we are talking about JavaScript and we understood the various interesting concepts of JavaScript. Here is the complete URL set.
- Advance JavaScript: History and role of JavaScript behind modern web
- Advance JavaScript: play with object in JavaScript
- Advance JavaScript: Function Definition Style in JavaScript
- Advance JavaScript: Understand undefined in JavaScript
- Advance JavaScript: Understand "class"-ical concept of JavaScript
- Advance JavaScript: Implement inheritance in JavaScript
- Advance JavaScript: Callback design pattern and callback function in JavaScript
- Advance JavaScript: Exception handling in JavaScript
- Advance JavaScript: Scope of Variable in JavaScript
- Advance JavaScript: closure in JavaScript
In this article we will understand the "undefined" keyword (Let's use the term keyword) of JavaScript. After reading this article you will understand the both keyword in and it's functionality.
The dictionary meaning of undefined is "not defined". In terms of JavaScript, it remains the same, although in JavaScript something that is not yet defined can be defined later. Before continuing with the explanation, one thing needs to be clarified. JavaScript is a loosely typed language. That means there is no pre-defined data type in JavaScript as in C, C++ or C#. And everything is possible by the "var" keyword. Yes, we can define an integer, string, boolean and many more types using the "var" keyword. The syntax is as in the following.
var a =100;
Very fine, the concept is clear. var is applicable for any data type. Now, the next concept is whether the "var" keyword can be transferred to an actual type? The answer is yes, it can be converted. Let's see that in an example. Have a look at the following code.
Let's understand "undefined"
var c = false;
var b ="Sourav";
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head id="Head1" runat="server">
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- var val1 = 100;
- var val2 = "sourav";
- alert("Type of val1:- " + typeof val1 + " Type of val2:- " + typeof val2);
- </script>
- </form>
- </body>
- </html>

Again, this is to clarify our understanding. At parse time var is converted into an actual type. In our previous article we saw that when we define any object in JavaScript, it attaches to the window object with practical implementation. The same is true for this example.
- <form id="form1" runat="server">
- <script>
- alert(abc);
- var abc = 100;
- </script>
- </form>

- <form id="form1" runat="server">
- <script>
- var val;
- alert("Status of val is :- " + val);
- </script>
- </form>

- <body>
- <form id="form1" runat="server">
- <script>
- function abc() {
- return;
- }
- alert("Return from abc function is:- " + abc());
- </script>
- </form>
- </body>

The output says that when we are not returning anything from a function then it's always "undefined".
Are undefined and null the same?
Undefined and null are not the same; undefined is not equal to null. Let's try with a sample example to check that.
- <form id="form1" runat="server">
- <script>
- alert(null === undefined)
- </script>
- </form>

The last but not least question is, is undefined an object or something else? No, undefined is not an object, it's a property of the object (please refer to w3cschools.com for farther reference).
In this article we saw the used of "undefined" in JavaScript, I hope you have understood that fact. Follow this article series to get more clarification on JavaScript.

Sourav KayalPosted Nov 8, 2013, 9:08 PM
yes sir, one dedicated article on "var" keyword will come soon..
Sam HobbsPosted Nov 8, 2013, 12:53 PM
Perhaps you could write another article explaining why it is a good practice to always declare a variable using "var" but I think in the context of this article "var" has little use. In JavaScript, the important thing is to assign a value to a variable. I think that for this article it would help to put more emphasis on assigning a value.
Sam HobbsPosted Nov 8, 2013, 12:43 PM
You say "No, undefined is not an object , it's a property of the object". Yet something that is not defined is not an object. Language specifications tend to be carefully written using the most relevant and precise words possible. The standard version of JavaScript is ECMAScript. The ECMAScript Language Specification calls "undefined" a type and it calls "undefined" a value but I do not see where it is called a property. The ECMAScript Language Specification is at: http://www.ecma-international.org/publications/standards/Ecma-262.htm