Ok so can we use a single variable 'x' for multiple purposes or I have define a new variable? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Ok so can we use a single variable 'x' for multiple purposes or I have define a new variable?

15th Jun 2017, 10:02 AM
Mayuresh Joshi
Mayuresh Joshi - avatar
2 Answers
+ 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.
15th Jun 2017, 10:07 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
16th Jun 2017, 8:13 AM
Harcau Ionut
Harcau Ionut - avatar