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 trial

JavaScript JavaScript Basics Working with Strings Introducing Strings Review

Josh O.
Josh O.
2,370 Points

Why is this wrong? "good morning, students".length(); I would rather save it as a variable & then do variable.lenght();

Why is this wrong? "good morning, students".length(); I would rather save it as a variable & then do variable.lenght();

2 Answers

Blaize Pennington
Blaize Pennington
13,879 Points

I'm not sure I follow your question, but .length does not need the parentheses.

let thisVariable = "Good morning, students";
console.log(thisVariable.length);
//returns 22
console.log("Good morning, students".length)
//returns 22
HIDAYATULLAH ARGHANDABI
HIDAYATULLAH ARGHANDABI
21,058 Points

Hello Josh, The length works with strings and array for example

const a = [1, 2, 4, 45]
// a.length => 4
const myName = "JOSH"
// myName.length => 4

I wish my comment helped you you can check the documentation : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length