Variables in JS | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Variables in JS

Is it necessary to put "var" in front of all variables in JS? Or "var" needs to be set only we declare a variable? How to do it right? var x=5; var x=x+5; Or var x=5; x=x+5; Or x=5; x=x+5;

31st Mar 2021, 9:02 AM
Yuri Makhanov
Yuri Makhanov - avatar
4 Answers
+ 2
Yuri Makhanov Declare once any use many times, assign value according to your requirements. No need to write var everytime because that variable will be new variable.
31st Mar 2021, 9:21 AM
A͢J
A͢J - avatar
+ 1
All 3 work. 1st case you are redeclaring x. Not recommended. 2nd case is most appropriate. 3rd case x is declared globally. Best is to use let and const keyword because of their block and function level scope.
31st Mar 2021, 9:23 AM
Avinesh
Avinesh - avatar
+ 1
Your 2nd way is what I'd recommend from the 3 examples but It'd suggest learning const and let too. Don't use the 3rd way as this can lead to bugs due to your variable being globally scoped, and you don't need to redeclare as in your 1st way. You're actually creating a new variable there.
31st Mar 2021, 9:26 AM
CamelBeatsSnake
CamelBeatsSnake - avatar
+ 1
When you put "var" (or, preferably in modern JavaScript, "let" or "const") in front of a valid variable name (such as "x" or "myVar" or "thisVarIsValidButKindaLong"), a bunch of things happen. Some of them are hidden by the JS runtime, but here (in some order; I dunno) they are: - a "box" is set aside to store the contents of your variable. In certain programming languages, and with certain variables, this box is given a certain size (i.e., in terms of memory). - that box is labeled with whatever label you give the variable. - finally, depending on the keywords - var, let, const for JS; private, public, static, etc. for Java and similar languages - that you provide, we determine who *can* and *cannot* access the variable (and thus the data inside it). This final bit is called "scoping" the variable. Next, let's discuss the actual question you ask. Should we put "var" in front of every variable? Definitely not! Remember, doing "<keyword> <variable name>" says to the runtime "I wanna make a *new* box with this value". If all you're doing is putting a new value in the same box (it's got the same label)... why build a whole new box? In addition, let's go back to that "scope" idea. Imagine I wanna get the sum of all values in a list (we call this an "array" in JS): var myArray = [1,3,5,7]; With your first method - re-using "var" every time we reassign a variable - here's what we might try: var sum = 0; for(var x of myArray){ var sum = sum + myArray; } But here's the issue: every time we do "var sum = sum..." inside that loop, we're throwing old the old "box" labeled "sum", and creating a completely new box. If we logged out the value of sum right after that line, we'd get: 1,3,5,7. Remember, our loop was supposed to *sum* the values! Let's look at what happens if we remove that keyword: var sum = 0; for(var x of myArray){ sum = sum + myArray; } Now, whenever that inner line runs, it refers to the current value of the "sum" variable which is stored outside the loop (and thus "pr
31st Mar 2021, 2:39 PM
HealyUnit
HealyUnit - avatar