Welcome to the "Advanced JavaScript" article series. In this series we are talking about JavaScript and we understood various interesting concept of JavaScript. Here is complete URL set.
In this article we will understand "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.
Let's understand "undefined"
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 eplanation, 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;
var b ="Sourav";
var c = false;
Very fine, the concept is clear. var is applicable for any data type. Now, the next concept is whether the "var" keyword can be transfered 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.
<%@ 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>
Here is the output.
We are using the typeof keyword to determine the type of val1 and val2 that we have declare using the "var" keyword in the program. And in the output we are seeing that the actual data type of val1 and val2 has been converted to a number and a string.
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 a practical implementation. The same is true for this example.
Now, the question is, what is the relationship with "undefined" and this example. Let's proceed with more examples.
<form id="form1" runat="server">
<script>
alert(abc);
var abc = 100;
</script>
</form>
Here is the sample output.
It's showing that abc is not defined though we have defined abc in the next line. This happens because JavaScript is not a compiled language. The alert is trying to find abc from the window object and when it's not finding it it's throwing that "abc" is not defined in the window object.
<form id="form1" runat="server">
<script>
var val;
alert("Status of val is :- " + val);
</script>
</form>
Unlike the previous example , here we have declared the "val" variable first then attempted to print it's value. It's undefined because the value id is still not defined as a variable.
If we do not specify any return value from a function in JavaScript then it always returns undefined. Let's see that in an example. Here is the sample code.
<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 an sample example to check that.
<form id="form1" runat="server">
<script>
alert(null === undefined)
</script>
</form>
The output says that they are not the same by showing false.
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).
Conclusion
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 of JavaScript.