All TalkersCode Topics

Follow TalkersCode On Social Media

How To Check Undefined In jQuery

Last Updated : Jan 1, 2023

How To Check Undefined In jQuery

In this article we will show you the solution of how to check undefined in jQuery, here we are going to show you example with the help of undefined statement and if else condition.

The undefined is a primitive value that specifies a variable has not been initialized a value or may not be declared as well.

To compare particular element with undefined value we need to use condition statement that’s way here used if else condition.

Step By Step Guide On How To Check Undefined In jQuery :-

Here we defined html block with empty paragraph element for display result on webpage.

In script block we defined ready method which is help you to load script code when opening this program on browser. Then defined two variables, here one variable has initial value and another doesn’t have for verifying purpose.

After that using if and else if we checks which variable has undefined value then appending respective message with p element by text() method.

<!DOCTYPE html>
        <title>check undefined</title>
        <script src=""></script>
                var x1;
                var x2="Hi";
                if(x2 == undefined){
                    $("p").text("X2 variable is undefined");
                else if(x1 == undefined){
                    $("p").text("X1 variable is undefined");
                    $("p").text("X1 and X2 variables defined");
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready then declared variables ‘X1, X2’ and X2 initialized with value ‘Hi’ but x1 doesn’t. If condition checks whether X2 value equals to ‘undefined’ then binding text ‘X2 variable is undefined’ with p element. In case if condition true then this message will display on webpage.
  8. Then else if() checks whether X1 value equals to ‘undefined’ if its true then message ‘X1 variable is undefined’ will display on webpage.
  9. Otherwise, else block message ‘X1 and X2 variables defined’ will display on front end.
  10. In body block we defined empty p element pair tag for result display on webpage.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to check undefined using jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes in document without any error.

When we executes this program on browser we can see ‘X1 variable is undefined’, because X1 is not initialized properly.

I hope this article on how to check undefined in jQuery helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials