All TalkersCode Topics

Follow TalkersCode On Social Media

Global Variable In jQuery

Last Updated : Jul 1, 2023

Global Variable In jQuery

In this article we will show you the solution of global variable in jQuery, here we are going to show you example with desire result. The jQuery global variable is a variable that is needs to define outside function.

In script blog you can use within any function because its global means public and defined global variable value printed on both webpage and console by respective html() and console.log() methods.

Step By Step Guide On Global Variable In jQuery :-

Here we defined html block with h2 tag for heading, button for generate specified source code and empty p tag for bind the result.

In script block we defined global variable and constructor then in constructor we initialized values for global variable.

Then dis() method defined for display actual json format values of global variable on webpage and console panel.

You can modify global variable name with values as per your need here shown for example only.

<!DOCTYPE html>
        <title>GLOBAL VARIABLE IN JQUERY</title>
        <script src=""></script>
            var Gvar;
               function initialGvar(){
            function dis(){
                    console.log('The Global Variable Name is '+Gvar[0].name+' and Her Age is '+Gvar[0].age);
                    $("p").html('The Global Variable Name is <b>'+Gvar[0].name+'</b> and Her Age is <b>'+Gvar[0].age+'</b>');
        <h2>Display Global Variable Value</h2>
        <button onclick="dis()">Click Here</button>
  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 global variable ‘Gvar’ then as usual ready method defined, which loads all script code when program on browser.
  8. There we initialized Gvar variable with two properties ‘name, age’ respective values ‘Dhanu, 25’ and at the end needs to call constructor for initialize values.
  9. After that defined dis() method, using if loop we checks whether global variable ‘Gvar’ has value or not. If returns true then we display all values of ‘Gvar’ variable otherwise error occurred.
  10. In body block we defined h3 heading with text ‘Display Global Variable Value’, button ‘Click Here’ with onclick() method contain ‘dis()’ method call and empty p tag to display result 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 define global variable 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 heading ‘Display Global Variable Value’, button ‘Click Here’ now user needs to click on button then you can see the global variable value on webpage as well console panel.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪