What is a variable? What is the difference between a local and global variable? Give an example of an HTML file with a JavaScript which declares and uses a variable.
What is a variable:
A variable is nothing but a name given to the storage area, which stores value and the program can manipulate that value according to the requirement and calculations.
Difference between local and global variable:
The main difference between local and global variables is that the local variables are declared within a function. Local variable is created when that specific function starts its executions. and the variable is lost when the function finishes its execution. local variables do not share data directly. the function has to return its value then only it can be used by the program.
on the other hand global variable are declared outside of the function. they are created when the program execution starts and it is lost when program execution finishes. global variables do share data and can be used anywhere in the program.
Step by step
Solved in 2 steps