- 1
Ok so can we use a single variable 'x' for multiple purposes or I have define a new variable?
2 odpowiedzi
+ 12
Let's say you want to program a TV remote control.
You will want to have a variable which holds the user input, and this variable will be responsible only for the user input.
var user_input;
Now, you will not want to use it to store other values such as channel frequency, because it isn't meant to store frequencies and you will be confused if you do.
But can you actually do so? Yes you can. It's just a matter of keeping things clear by using different variables for different purposes.
+ 1
Also, it depends if you want to declare a global variable (which can be used in all the functions) or you are declaring a variable in a function.
If you have function A and function B, both lets say are returning a sum, you can declare 2 variables sum, on for each function. They are "local" variables and are available just in the function you are creating them.