.length
`.length` tells you how many characters a string has or how many items an array has.
.length is a property, not a method: you write it without parentheses. On a string it counts characters (in UTF-16 units). On an array it counts items, and it is always the highest index plus one.
The result is a number, so you can compare it, add it or use it in an if. The type checker knows it is a number and warns you if you call it like a function. Types are gone when Node runs the code, but .length stays because it is real JavaScript.
In the course you see it from lesson 00.02, in lines like console.log(language.length, lessons, finished, words.length);, where it measures a string and an array. You use it to check whether there is data, to loop with an index, or to validate sizes.
const words = ["a", "b"];
console.log(words.length);
Reach for it when: you need the size of a string or an array, for example to check it is not empty or to set a loop limit.
Examples
Common mistake
const words = ["a", "b"]; console.log(words.length());
Why: `length` is a number, not a function. With parentheses you try to call a number.
How to write it: Write words.length without parentheses.
Common mistake
const words = ["a", "b", "c"]; console.log(words[words.length]);
Why: The last index is `length - 1`. The index `length` is outside the array and gives `undefined`.
How to write it: Use words[words.length - 1] or words.at(-1).
