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.
Popular Classes
Scratch Coding
Minecraft Coding
TinkerCAD
Roblox Studio
Python for Kids
Javascript for Kids
Pre-Algebra
Geometry for Kids
Copyright @ Kodeclik 2025. All rights reserved.
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.
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:
Attempting to modify a character by index has no effect:
Any transformation produces a new 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.
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:
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.
To count Unicode code points (what most people think of as characters), iterate by code points:
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()).
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.
const name = "Kodeclik";
name[0] = "C"; // no change
console.log(name); // "Kodeclik"
const upper = name.toUpperCase(); // "KODECLIK"
console.log(name); // "Kodeclik" (unchanged)
const name = "Kodeclik";
console.log(name.length); // 8
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)
const s = "😄";
console.log([...s].length); // 1