What are these lines of code for

Tell us what’s happening:
Hi All

Okay so I don’t get why the very first line of code is set to zero, how does this relate?
Is it a default? Should I have known about this?

And why am I not seeing anything come up although there are no alerts

Your code so far


// Setup
var lastNameLength = 0;
var lastName = "Lovelace";

// Only change code below this line

lastNameLength = lastName.length;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 Edg/88.0.705.56.

Challenge: Find the Length of a String

Link to the challenge:

That’s called initializing the variable. It’s just giving it a default or starting value. In this case that initial value is never used.

1 Like

when a variable is initialized it’s normal to put in a value that is of the same type of the end result, in this case 0 is pretty neutral for a number, and the final result is a number

there is no new coding concepts in this line


the code does not automatically show anything to you, you need to tell it you want something shown - some challenges are set up with that, in most cases you need to use a command like console.log()

if you write console.log(lastNameLength) you see the value of that variable in the console

1 Like

What would be the outcome if we never initialise it? Thanks for the speedy reply.

if you delete just the assignment, nothing changes - it’s good practice to give a starting value even if not used to say what’s the data type

try deleting the whole var lastNameLength = 0; line to see what happens too

Go ahead and try it. You’re tests won’t all pass, but you can see how the end result is effected.