Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialEnemuo Felix
1,895 PointsVariable declaration
Ok, I know i should have asked this in earlier courses but i thought i would grasp the logic as i progress. Here's the question, I'm confused on how and when to use both, The difference and the idea?
var count;
var count = " ";
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! That's actually a bit of a more advanced question than you think it is. And what I think you actually mean is this:
var count; // this a variable with no data type... yet
var count = ""; // this is an empty string
var count = " " // this is a string containing a space
I'm going to put up some sample code and I want you to see what happens in the console. I think you'll be surprised by the first example.
var x; // this variable is of type undefined... right now
console.log(x + 2);
console.log(x + "hello");
var y = ""; // this variable contains an empty string
console.log(y + 2);
console.log(y + "hello");
var z = 4; // this variable holds a number
console.log(z + 2);
console.log(z + "hello");
Essentially, when you say var count;
you are defining a variable that will later contain some sort of data. In many languages, you must explicitly specify what kind of data it will be holding. JavaScript is a little special and has something it calls "type inferencing" which means it makes a guess about what kind of data a variable will hold depending on how you're using it. But when it's never given an initial value, it can't make that guess. That's all fine and dandy until you try to use the +
or some other mathematical function on it. It can combine a string and an integer. It can combine 2 integers. It can combine 2 strings. What it can't really do is combine something that is undefined and any one of those because it doesn't know what the result is supposed to be at the end.
So if you are guaranteed to give the variable an initial value later before you start manipulating the value with concatenation or mathematical operators then you don't need to give it an initial value. That being said, I generally give values that I know for a fact should hold a string later in my code an initial empty string value so that I don't run into just this scenario down the line.
Hope this clarifies things!
Enemuo Felix
1,895 PointsEnemuo Felix
1,895 PointsThank you Jennifer. I think i'm trying to grasp the idea a little bit now. Here's how i understood it;
Please, let me know if I'm on the right track?