Tell us what’s happening:
I didn’t really understand this:
var lastNameLength = 0;
I checked the W3 School page about it.
So why do you need to define the lastNameLength to 0 when an empty string results in 0 by default? Or am I completely missing the concept here? I have the feeling understanding this will prove pretty crucial later on.
Your code so far
var firstNameLength = 0;
var firstName = "Ada";
firstNameLength = firstName.length;
var lastNameLength = 0;
var lastName = "Lovelace";
// Only change code below this line.
lastNameLength = lastName.length;
**Link to the challenge:**
It is a good practice to initialize any variable that you create so that if it is referenced somewhere in the code it will have a value other than
undefined. That’s the main reason. You are correct that an empty string will result in a zero length and set that value in the variable. It is a good practice to initialize variables, though, and that’s probably the point of doing that here.
I just looked at the Beta exercises and I see a couple that are related to creating and initializing variables
Storing values with the assignment operator and Understanding unitialized variables
I hope that helps!
Thanks! I am currently going through the beta site, but haven’t gotten to that part yet But this clears things up already. I was afraid I was missing some important point.
It looks like those lessons are preceding the one that you asked about - maybe you should see if you already did those and just didn’t get the point about initialization? Sometimes a concept doesn’t “click” right away until you actually see it in use and (as you found here) have a question about it…
Hmm, they did precede. I did not get the point of initialization then. The connection between the challenges didn’t really click. Still not sure how those two challenges would answer the question I had My example isn’t undefined. It returns a zero if it’s undefined, and not the string “undefined” like mentioned in the Understanding Uninitialized Variables. I will have to chew on it some more
Oh well, at least I know it’s good practice.
I agree that in this case there doesn’t seem to be too much sense in initializing it, but the general principle is that you should always initialize variables when you create them. I think that it would be helpful, at times, if the lessons that follow those could reinforce important points that were made in preceding lessons. But that could be a never-ending cascade of adding information to refer back to. I can see, perhaps, that they could reinforce the point of initialization by testing that you created and initialized the variable?
It’s kind of interesting in that if you go and remove the
=0; from the declaration it passes the first test, but fails the second? BUT the error message text doesn’t appear to have anything to do with initialization. I guess you would have to investigate what the testing sequence was to understand that…
Anyway - initialize your variables is the way to go!