JavaScript Global Variable:-
1.Global Variable:-
In contrast, global variables are variables that are defined outside of functions. These variables have global scope, so they can be used by any function without passing them to the function as parameters.
Example:-
!DOCTYPE html><html> <body> <center> <h1 style="color:green;">GeeksforGeeks</h1> <p>Outside myfunction() petName is not defined.</p> <p id="Geeks"></p> <p id="geeks"></p> <script> myfunction(); function myfunction() { var petName = "Sizzer"; // local variabl document.getElementById("Geeks").innerHTML = typeof petName + " " + petName; } document.getElementById("geeks").innerHTML = typeof petName; </script> </center> </body> </html>Output:-

2. Local Variable:-
When you use JavaScript, local variables are variables that are defined within functions. They have local scope, which means that they can only be used within the functions that define them.
Example:-
<!DOCTYPE html><html> <body> <center> <h1 style="color:green;">GeeksforGeeks</h1> <p>A GLOBAL variable can be accessed from any script or function.</p> <p id="geeks"></p> <p id="Geeks"></p> <script> var petName = "Rocky";//global variable myFunction(); function myFunction() { document.getElementById("geeks").innerHTML = typeof petName + "- " + "My pet name is " + petName; } document.getElementById("Geeks").innerHTML = typeof petName + "- " + "My pet name is " + petName; </script> </center> </body> </html>Output:-
