Example 1 — Basic Length
Read length on a greeting, an empty string, and text with spaces.
const greeting = "Hello, World!";
console.log(greeting.length); // 13
console.log("".length); // 0
console.log(" spaces ".length); // 10 How It Works
- Count every unit.
"Hello, World!"has 13 code units — letters, comma, space, and!. - Empty means zero.
""has no units, solengthis0. - Spaces count. Leading and trailing spaces still add to
length(10here). - No method call. Use
.length, not.length().

