How to define global variable in a JavaScript function?



To declare a global variable, use the var at global scope −

<script>
   var myGlobalVariable;
   function display() {
      //
   }
</script>

For JavaScript function, assign the property to a window −

<script>
   function display() {
      window. myGlobalVariable = …;
   }
</script>
Updated on: 2020-06-23T11:56:54+05:30

403 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements