Updated April 19, 2023
Introduction to jQuery undefined
The jQuery undefined is a primitive value that specifies that a variable has not been initialized a value or may not be declared as well. The jQuery undefined is a built-in primitive data type in jQuery. The jQuery there are seven primitive data types like number, string, boolean, bigint, undefined, null, and symbol, so undefined is one of the primitive data types in jQuery.
In jQuery, if a variable is declared but the value is not assigned to it, then an undefined value is assigned to the variable automatically. Therefore, when we try to display this variable value, the undefined word will be displayed, so which means the undefined is an unknown value or lack of value or not provided value. The null is also a primitive data type that specifies no value, which means the null value means the absence of a value or no value and undefined value means the variable has been declared but the value is not assigned.
Syntax of jQuery undefined Primitive Data Type
Given below is the syntax mentioned:
var myVariable;
if(myVariable == 'undefined')
{
// some code to execute
}
Or
// with strict equality operator
if(myVariable === 'undefined')
{
// some code to execute
}
Working of jQuery undefined Primitive Data Type
- The jQuery undefined primitive data is assigned automatically to a variable that is declared but not initialized by any value.
- Suppose we have a “phoneNo” variable that is declared as “ var phoneNo;”, but not initialize.
- Now when we display the variable as “alert(phoneNo);”, the alert message will display “undefined”, because the value is not assigned to the variable “phoneNo”.
Examples
Given below are the examples :
Example #1
Example of jQuery primitive data type to display the uninitialized value of the variable.
Code:
<!doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<script src = "https://code.jquery.com/jquery-3.5.0.js"> </script>
<title> This is an example for jQuery undefined primitive data type </title>
<script>
function disp()
{
var x;
alert( "The variable value is : " +x );
}
</script>
</head>
<body>
<h3> This an example of undefined primitive data type : </h3>
<button onclick = "disp()"> Click to know whether the variable is defined or not. </button>
</body>
</html>
Output:
Once we click on the “click to know whether the variable is defined or not” button:
In the above code, when we click on the button the disp() function get execute. In the disp() function the variable x is declare but not assigned any value to it. Next, the x variable is displaying in the alert, as we can see in the output the value of the x is “undefined” displaying as it is not assigned any value.
Example #2
Example of jQuery primitive data type to value of the variable with the undefined primitive value.
Code:
<!doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<script src = "https://code.jquery.com/jquery-3.5.0.js"> </script>
<title> This is an example for jQuery undefined primitive data type </title>
<script>
function disp()
{
var x;
var y = 56;
if ( typeof x === "undefined") {
message = "Variable x is undefined";
} else {
message = "Variable x is defined";
}
if ( typeof y === "undefined") {
message = message + " and Variable y is undefined";
} else {
message = message + " and Variable y is defined";
}
$( "div" ).text( message );
}
</script>
</head>
<body>
<h3> This an example of undefined primitive data type : </h3>
<button onclick = "disp()"> Click to know whether the variable is defined or not. </button>
<div style = "color:red;" > </div>
</body>
</html>
Output:
Once we click on “click to know whether the variable is defined or not” button:
In the above code, when we click on the button the disp() function gets to execute. In the disp() function, there are two variables x and y, where x is just declared but not assign any value and y is initialized with 56 value. Next, the x and y variables are comparing with the “undefined” primitive by using the “typeof” operator. The x variable automatically gets the “undefined” value so the condition becomes true whereas y has some value so the condition becomes false for it and the message is displaying according to this.
Example #3
Example of jQuery primitive data type to show passing the undefined variable.
Code:
<!doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<script src = "https://code.jquery.com/jquery-3.5.0.js"> </script>
<title> This is an example for jQuery undefined primitive data type </title>
<script>
function add( x, y)
{
$( "div" ).text( "The value of x is : " + x +", the value of y is : " +y + " and the result of x and y is : " + (x+y) );
}
function disp()
{
var n1 = 10;
var n2;
add(n1, n2);}
</script>
</head>
<body>
<h3> This an example of undefined primitive data type : </h3>
<br>
<button onclick = "disp()"> Click to get addition of two numbers. </button>
<div style = "color:red;" > </div>
</body>
</html>
Output:
Once we click on the “click to get addition of two numbers” button:
In the above code, in the disp() function it is calling to the add() function by passing the two variables n1 and n2, where n1 is initialized with 10 and n2 is just declare but does not assign any value (automatically gets the “undefined” value). Next in the add() function the n1 and n2 variables addition is displaying which is “NAN”(Not-a-Number).
Conclusion
The jQuery undefined primitive value is a built in data type, which specifies that a variable has not been initialized a value or may not be declared as well.
Recommended Articles
This is a guide to jQuery undefined. Here we discuss the introduction, working of jQuery undefined primitive data type and examples. You may also have a look at the following articles to learn more –