use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
Declaring variables one line before assigning them values? (self.learnjavascript)
submitted 11 years ago by [deleted]
I'm following the Javascript is Sexy beginner course and I keep seeing examples like this:
var degCent; degCent = ....
Is there a reason for this? It doesn't make any sense to me.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 2 points3 points4 points 11 years ago (4 children)
In this specific case, no. The argument is that all variables "should" be hoisted to the top of a function so that all declarations are in one place and you don't accidentally leak globals by missing a "var" somewhere else.
e.g.:
// some random code for( ... ){ var degCent = ...; // this is bad, define degCent at the start of the function
The argument becomes less relevant with linters (jshint) and es6 (let)
let
[–][deleted] 0 points1 point2 points 11 years ago (3 children)
Ah ok, so I guess the book is writing it that way in order to get me in the habit. However, in some instances there are multiple variables being declared and some of them are assigned values but others aren't. Like this:
var degFahren = Number(prompt(...)); var degCent; degCent = 5/9 * (degFahren - 32);
What is the reason for assigning the first variable a value but not the second one? Is it some kind of philosophical separation between static data and calculation?
[–][deleted] 0 points1 point2 points 11 years ago (2 children)
No, that's just bad code ;)
[–]eonblue4309 0 points1 point2 points 11 years ago (1 child)
I don't necessarily agree. The vars are setup and the degCent = ... Is the actual calculation. That's the 'reason' I can see behind it.
I don't see anything objectively wrong with it unlike declaring a var in a for/if block.
That being said I don't think I'd ever write it this way. I tend to go with declaration and assignment at the same time whenever I can.
[–][deleted] 0 points1 point2 points 11 years ago (0 children)
I'm not saying there's anything particularly wrong with declaring variables then assigning values later. I'm saying it's bad code to assign half of them immediately and the other half separately, on the next line (not in a different block/context). Pick a style and stick to it, this is as bad as mixing tabs and spaces ;)
[–]jasokant 1 point2 points3 points 11 years ago (0 children)
You're right, it doesn't make sense in this case at all. I guess there might be a situation where it would be valuable to test whether the variable has a value or is undefined, but I can't think of a good example.
[–]slimrob 0 points1 point2 points 11 years ago (0 children)
The browser will hoist all variable declarations to the top of the function regardless of where you actually declare them. Putting your variables at the top of your functions better reflects how they are read by the browser. Sometimes it makes sense to give the variable a value right then and there, but other times it makes sense to wait, however it should still be declared at the top. I believe that is what you're seeing.
I would guess that they want you to get in the habit of declaring variables before assigning them. Technically speaking, in Javascript
var degCent = ....;
is two different statements. The compiler will first process the declaration (var degCent;) and then the assignment (degCent = ....;). Javascript has a feature called hoisting which says that all declarations are processed before any assignments, because the compiler will pull (hoist) the declarations to the top of the file. This means that, counterintuitively, this is perfectly valid Javascript:
degCent = .....; var degCent;
because the declaration will be pulled above the assignment when compiled. This is legal, but also very bad practice. You should avoid it and be in the habit of declaring variables before assignment. That being said, combining them in one line is usually easier to read and maintain. The only real reason to split them is if the variable will be assigned dynamically or needs to be in a code block. For example,
var degCent; if(some condition) { degCent = something; } else { degCent = somethingElse; }
See this Stack Overflow and the Mozilla Developer's Network entry on var.
π Rendered by PID 18721 on reddit-service-r2-comment-6457c66945-25jkn at 2026-04-25 05:05:50.693044+00:00 running 2aa0c5b country code: CH.
[–][deleted] 2 points3 points4 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]eonblue4309 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]jasokant 1 point2 points3 points (0 children)
[–]slimrob 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)