Basic JavaScript - Find the Length of a String

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

// Setup
let lastNameLength = 0;
const lastName = "Lovelace";

// Only change code below this line
lastNameLength = lastName;
console.log(lastNameLength.length);

Your browser information:

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

Challenge: Basic JavaScript - Find the Length of a String

Link to the challenge:

If the problem is that it’s not passing the tests for you. The answer is because the code isn’t using the .length on the lastName. It’s not processing and returning that in the code. call lastName.length inside a console.log() means it just displays the outcome in the console.

1 Like

I DONT GET IT I TRIED ALL
console.log(lastName.length);
console.log(lastNameLength.length);

1 Like

you can change the existing last line, (below //change code below)

1 Like

change it with something like what for exemple

We all know that what is in the right of the assignment operator done first then the Value saved in the variable … so we here want to assign “lastName” length in the “lastNameLength” variable … so we first go to get the length by adding .length to the “lastName” and assign it to lastNameLength … that can be done like that lastNameLength = lastName.length; … then we can console.log the variable that contains the length … console.log(lastNameLength); just like that … I wish I explained it well

1 Like

the code not passing test case is just because it is expecting ‘lastNameLength’ should be assign by correct string(lastName) length.
Your code didn’t have issue its just test case (see 2 TC),if you want to log also following will work
lastNameLength = lastName.length;

console.log(lastNameLength);

2 Likes

Console.log displays, or shows you what the code is doing, it’s a bit like a window into what certain operations do or would do.

The challenge wants you to basically add .length to the code that is already there.
For example:

// Setup
let lastNameLength = 0;
const lastName = "Lovelace";

// Only change code below this line
//lastNameLength currently = 0

lastNameLength = lastName.length;
//Here we change the variable lastNameLength = lastName.length
// then if you want to see it in the console you can call a console.log()
console.log(lastNameLength)

In your code the console.log(lastName.length) doesn’t assign any value to anything, it doesn’t assign anything to the variable lastNameLength it just shows you what lastName.length is. It’s a useful tool for debugging

You could also reassign the variable lastNameLength inside a console.log() to have it shown in the console, but it’s not best practice

1 Like

i was thinking same, but i’m not sure it’s accurate. it’s the best way to think about it at this stage i suppose.
a console log will run code if you call a function, but it just displays what’s returned from that function. if the function happens to change something in the code that will stand. i think.
in this case the console log calls the .length function but that returns the corresponding length without assigning it to a variable.
although i may be wrong in calling .length a function itself. i’m n00b.

1 Like

Yeah, for sure you could reassign the variable within a console.log(), but I wouldn’t do that. It’s not a good practice, as you’ll likely want your variables and any changes you make to them to persist beyond console.logs, which are often changed or deleted over time

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.