Kodeclik Logo

Learn More

Fall 2025

Kodeclik Blog

Finding the length of a Javascript string

In your Javascript journey, you will find a need to measure the length of a string. While there is an easy way to do it, the definition of length can be surprisingly nuanced as we will see.

This guide explains how length is defined, how to compute it with built-in features, and how to handle complex string examples with confidence.

Javascript string length

What is a Javascript string?

In Javascript, a string is one of the basic data types. Internally, a string is simply a sequence of characters. Strings are immutable, meaning its contents cannot be changed after creation. Thus, any “modification” produces a new string value rather than altering the original one.

For instance, “Kodeclik” is a Javascript string defined by the following code:

const name = "Kodeclik";

Attempting to modify a character by index has no effect:

name[0] = "C"; // no change
console.log(name); // "Kodeclik"

Any transformation produces a new string:

const upper = name.toUpperCase(); // "KODECLIK"
console.log(name);  // "Kodeclik" (unchanged)

What is the length of a Javascript string?

The length of a Javascript string is usually the number of characters it contains. For instance, “Kodeclik” has 8 characters and thus the string is of length 8.

How do we find the length of a Javascript string?

The built‑in way to compute the length of a Javascript string is the read‑only length property on string instances. This property returns the number of characters in the string, including spaces, punctuation, and special characters.

For instance, try:

const name = "Kodeclik";
console.log(name.length); // 8

Is length always the same as the number of characters?

The String length property returns a non-negative integer equal to the count of 16‑bit code units in the string’s internal UTF‑16 representation. What this means is that sometimes characters might display as a single character but internally might contribute 2 to the overall string length.

As a result, an emoji like “😄” has length 2 even though it looks like a single character, because it is encoded as two UTF‑16 code units.

const s = "😄";
console.log(s.length);        // 2
console.log(s.charCodeAt(0)); // first 16‑bit code unit (high surrogate)
console.log(s.charCodeAt(1)); // second 16‑bit code unit (low surrogate)

To count Unicode code points (what most people think of as characters), iterate by code points:

const s = "😄";
console.log([...s].length); // 1

String.length is a property, not a method

Note that length is a property of the String object, not a function or method. Therefore, it is accessed directly without parentheses (e.g., myString.length instead of myString.length()).

How fast is the length computation?

Accessing the length property is a fast operation as the length is typically stored and not calculated each time it's accessed.

Want to learn Javascript with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2025. All rights reserved.