Local variables:
- Defining a variable inside the function or block is called Local variable.
- We can access them only from the same block or function.
<html> <head> <script> function add(){ var a=10; var b=20; var c=a+b; document.write(“Sum is : ” + c); } </script> </head> <body> <button onclick=”add()”>add</button> </body> </html> |
Global variables: Defining variables outside to all functions – we can them from all functions.
<html> <head> <script> var a=5; var b=3; function add(){ document.write(“Sum is : ” + (a+b)); } function subtract(){ document.write(“Difference is : ” + (a-b)); } function multiply(){ document.write(“Product is : ” + (a*b)); } </script> </head> <body> <button onclick=”add()”>add</button> <button onclick=”subtract()”>subtract</button> <button onclick=”multiply()”>multiply</button> </body> </html> |