Course offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov (not interview packs)

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.

BeginnerJavaScript String ProgramsExample 1 of 5
string-length.js
1// Method 1: Using length property
2let str = "Hello World";
3console.log("String:", str);
4console.log("Length:", str.length);
5
6// Method 2: Manual count using loop
7function stringLength(str) {
8 let count = 0;
9 for (let i = 0; i < str.length; i++) {
10 count++;
11 }
12 return count;
13}
14
15console.log("\nManual count:", stringLength("JavaScript"));
16
17// Method 3: Using split and length
18function stringLengthSplit(str) {
19 return str.split('').length;
20}
21
22console.log("Using split:", stringLengthSplit("Programming"));
23
24// Method 4: Count without spaces
25function stringLengthNoSpaces(str) {
26 return str.replace(/\s/g, '').length;
27}
28
29console.log("\nLength without spaces:", stringLengthNoSpaces("Hello World"));
30
31// Method 5: Count words
32function countWords(str) {
33 return str.trim().split(/\s+/).length;
34}
35
36console.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. 1

    "Hello World".length is 11.

  2. 2

    The loop counts 10 characters in "JavaScript".

  3. 3

    split gives 11 for "Programming".

  4. 4

    Removing spaces gives 10.

  5. 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.