Updated July 10, 2023
Introduction to JavaScript undefined
If you want to understand the definition of undefined in JavaScript, you must know the definition of declaration and definition in JavaScript. Declaring a variable in JavaScript with the keyword var is said to be a declaration.
Example: var name;
- Definition: People say assigning a value to a declared variable in JavaScript is the definition.
Example: name=10;
- Undefined: JavaScript does not define a declared variable and considers it undefined. We have not assigned any value to the declared variable.
Example: var name; //no value the variable name
How does undefined work in JavaScript?
As JavaScript is loosely typed (a type of variable is not important), if we do not assign any value to a variable, then automatically JavaScript machine assigns an undefined value to it.
Syntax:
a. var book;
b. var obj={};
c. var array=[];
d. var fun=function()
{
return;
};
e. var fun=function(a)
{
return a;
}
fun();
Explanation:
- In Syntax, a variable book is not defined, so the JavaScript machine assigned undefined as its value.
- In Syntax b object variable obj is not defined, so the JavaScript machine assigned undefined as its value.
- In Syntax, the c array variable array is not defined, so the JavaScript machine assigned undefined as its value.
- In the Syntax d function variable, fun is not returning any value, so the JavaScript machine assigned undefined as its value.
- In Syntax e function variable fun is trying to return an undefined value, so the JavaScript machine assigned undefined as its value.
- The best way to compare value is the undefined value or not in JavaScript by using the typeof keyword.
Examples of JavaScript undefined
Examples of the following are given below:
Example #1 – Returning undefined variable
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var name;
document.write("My name is : "+name);
</script>
</body>
</html>
Output:
Explanation:
- In the above code, we tried to print an undefined variable name, so we got the output My name is: undefined.
Example #2 – Assigning a value to an undefined variable
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var name;
if(typeof name==='undefined')
{
name="Amardeep";
}
document.write("My name is : "+name+" after reassing");
</script>
</body>
</html>
Output:
Explanation:
- We are checking first name variable is “undefined” or not with the type operator
- If the condition becomes true, the programmer has not defined the “name” variable.
- name variable reassigns to String “Amardeep.”
- Printing the reassigned value in the last line.
Example #3 – Printing undefined object value
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var obj={};
document.write("My object name is : "+obj.name);
</script>
</body>
</html>
Output:
Explanation:
- In the above code, we are trying to print an undefined object value name from obj, so we got an undefined value as output.
Example #4 – Assigning a value to the undefined object
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var x={};
if(typeof x.age==='undefined')
{
x.age=24;
}
document.write("My object value is : "+x.age+" after reassigning age");
</script>
</body>
</html>
Output:
Explanation:
- We are checking whether the first age object is “undefined” or not with the typof operator.
- If the condition becomes true, the programmer has not defined the age variable.
- The age variable reassigns to the number 24.
- Printing the reassigned value in the last line.
Example #5 – Printing undefined array value
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var x=[];
document.write("My array value is : "+x[0]);
</script>
</body>
</html>
Output:
Explanation:
- In the above code, we are trying to print the undefined array value of x[0] from the x array, so we got the undefined value as output.
Example #6 – Assigning a value to an undefined array index
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var x=[];
if(typeof x[0]==='undefined')
{
x[0]="Paramesh";
}
document.write("My array value is : "+x[0]+" after reassinging");
</script>
</body>
</html>
Output:
Explanation:
- We are checking first x[0] value is “undefined” or not with typof
- The x[0] array value is not defined if the condition becomes true.
- X[0] variable reassigns to String “Paramesh.”
- Printing the reassigned value in the last line.
Example #7 – Returning an undefined value from the function
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var x=function()
{
return;
};
var output=x();
document.write("My function value is : "+output);
</script>
</body>
</html>
Output:
Explanation:
- In the above code, we are trying to print an undefined function value, so we got an undefined value as output.
Example #8
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var x=function()
{
return;
};
output=x();
if(typeof output==='undefined')
{
output="Hi"
}
document.write("My function value is : "+output+" after reassigning");
</script>
</body>
</html>
Output:
Explanation:
- We are checking first x() function value is “undefined” or not with typof
- The condition cannot become true because we have not defined the value of the x() array.
- x() function output stored in a variable name with output.
- Output variable reassigns to String “Hi.”
- Printing the reassigned value in the last line.
Example #9
Code:
<!DOCTYPE html>
<html>
<body>
<script>
var x=function(number)
{
return number;
};
output=x();
if(typeof output==='undefined')
{
output="I am reassigned"
}
document.write("My function value is : "+output);
</script>
</body>
</html>
Output:
Explanation:
- First, we define a function with a parameter.
- We are checking first if the x() function value is “undefined” or not with typof
- The x() function does not define the array value because it does not receive the number argument. Therefore, if the condition becomes true, an error will occur.
- x() function output stored in a variable name with output.
- Output variable reassigns to String “I am reassigned.”
- Printing the reassigned value in the last line.
Conclusion
JavaScript all declared and not defined variables automatically assigned to undefined value through JavaScript Machine. The best way to check whether a variable or function is undefined is by using a typeof operator.
Recommended Articles
This is a guide to JavaScript undefined. Here we discuss how undefined works in JavaScript, along with appropriate Syntax and respective examples. You can also go through our other related articles to learn more–