How to define global variable in a JavaScript function?

JavascriptObject Oriented ProgrammingFront End Technology

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>
raja
Updated on 23-Jun-2020 11:56:54

Advertisements