String Length in JavaScript
Find string length in JavaScript with .length, a loop and split. Count characters, count words and see why emoji count as 2. Explanation and output.
1// Method 1: Using length property2let str = "Hello World";3console.log("String:", str);4console.log("Length:", str.length);56// Method 2: Manual count using loop7function stringLength(str) {8 let count = 0;9 for (let i = 0; i < str.length; i++) {10 count++;11 }12 return count;13}1415console.log("\nManual count:", stringLength("JavaScript"));1617// Method 3: Using split and length18function stringLengthSplit(str) {19 return str.split('').length;20}2122console.log("Using split:", stringLengthSplit("Programming"));2324// Method 4: Count without spaces25function stringLengthNoSpaces(str) {26 return str.replace(/\s/g, '').length;27}2829console.log("\nLength without spaces:", stringLengthNoSpaces("Hello World"));3031// Method 5: Count words32function countWords(str) {33 return str.trim().split(/\s+/).length;34}3536console.log("Word count:", countWords("Hello World from JavaScript"));
Sample Output
String: Hello World Length: 11 Manual count: 10 Using split: 11 Length without spaces: 10 Word count: 4
In this one
- How .length counts characters
- How to count without spaces using replace
- How to count words with trim and split
- Why emoji can make length look wrong
How it works
- 1
"Hello World".length is 11.
- 2
The loop counts 10 characters in "JavaScript".
- 3
split gives 11 for "Programming".
- 4
Removing spaces gives 10.
- 5
Splitting on spaces counts 4 words.
Edge cases
Emoji
'๐'.length is 2.
Many emoji use two UTF-16 units. Use [...str].length for a better count.
Empty string word count
countWords('') returns 1.
''.split(/\s+/) gives [''], one empty item. Check for an empty string first.
Extra spaces
countWords(' hi there ') returns 2.
trim and \s+ handle extra spaces correctly.
What's going on
This program measures strings. The length of a string is how many characters it has. A form might limit a username to 15 characters, or a bio to 160.
Method 1 reads str.length. "Hello World" has 11 characters, because the space counts too. length is a property, so you do not use brackets after it.
Method 2 counts by hand with a loop. It adds 1 for every character, so "JavaScript" gives 10. It shows what length means, though in real code you would just read length.
Method 3 splits the string into an array of characters with split('') and reads the array length.
Method 4 removes whitespace first. replace(/\s/g, '') uses a regular expression, a text pattern. \s means any space, tab or new line, and g means "everywhere". "Hello World" without the space has 10 characters.
Method 5 counts words. trim() removes spaces at both ends. split(/\s+/) splits on one or more spaces. Then the array length is the word count.
There is one trap. length counts UTF-16 code units, not the characters you see. Most letters use one unit, but many emoji use two. So '๐'.length is 2. [...str].length counts code points, which is closer to what people see. For plain text, .length is fine.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is "abc def".length?
Pick an answer to check it.
Question 2
What does " a b ".trim() return?
Pick an answer to check it.
Questions people ask
Is length a method?
No, it is a property. Write str.length, not str.length().
Do spaces count?
Yes. Every space is a character.
How do I limit input to 15 characters?
Check input.length <= 15, or use maxlength on the HTML input.