JavaScript String length Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

String instance length is a read-only data property that returns how many UTF-16 code units the string holds. It never mutates the string (ES1+).

Read the notes below before you rely on this property. Prefer feature detection when support is limited.

Syntax

Use string.length. Start with this line.

JavaScript
string.length
CallWhat it does
string.lengthPrimary signature.

Built into JavaScript. Read string.length (or a string literal) — no import required.

Library declarations
JavaScript
string.length

Parameters

ParameterDescription
.lengthRequired. UTF-16 unit count (read-only)
Grapheme toolsOptional. Needed only for emoji-accurate UI counts

Return Value

Return ValueDescription
*Non-negative integer (UTF-16 code units)

JavaScript Examples

Example 1 — Basic Length

Read length on a greeting, an empty string, and text with spaces.

JavaScript
const greeting = "Hello, World!";

console.log(greeting.length);      // 13
console.log("".length);            // 0
console.log("  spaces  ".length);  // 10
Try It Yourself

How It Works

  1. Count every unit. "Hello, World!" has 13 code units — letters, comma, space, and !.
  2. Empty means zero. "" has no units, so length is 0.
  3. Spaces count. Leading and trailing spaces still add to length (10 here).
  4. No method call. Use .length, not .length().

Example 2 — Min / Max Validation

Reject passwords that are too short or too long using length.

JavaScript
function checkPassword(password) {
  if (password.length < 8) {
    return "Too short";
  }
  if (password.length > 128) {
    return "Too long";
  }
  return "OK";
}

console.log(checkPassword("short"));
console.log(checkPassword("mypassword"));
Try It Yourself

How It Works

  1. Read size once. password.length is a fast integer compare — no regex needed for size rules.
  2. Enforce a minimum. "short" has length 5, so the function returns "Too short".
  3. Accept in range. "mypassword" has length 10, between 8 and 128 → "OK".
  4. Keep strength separate. Pair size checks with other rules (digits, symbols) as needed.

Example 3 — Emoji vs Code Points

Compare length with spread syntax when emoji are involved.

JavaScript
const heart = "💖";

console.log(heart.length);       // 2
console.log([...heart].length);  // 1

const text = "A💖Z";
console.log(text.length);        // 4
console.log([...text].length);   // 3
Try It Yourself

How It Works

  1. UTF-16 units. "💖" is stored as two code units, so .length is 2.
  2. Spread by code point. [...heart] uses the string iterator → one element → length 1.
  3. Mixed text. "A💖Z" is 4 units (A + pair + Z) but 3 code points.
  4. UI tip. For tweet-style limits, prefer Intl.Segmenter when grapheme accuracy matters.

Browser & Runtime Support

Instance String.length is a core language feature available since the earliest editions of JavaScript (ES1).

✓ Baseline · ES1+

String.length

Supported in every browser and all Node.js versions. No polyfill needed. Intl.Segmenter for grapheme counts requires modern browsers.

99% Universal API
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
String.length Excellent

Bottom line: Safe everywhere for basic counting. Use Intl.Segmenter when emoji-accurate user limits matter and you can target modern runtimes.

Frequently Asked Questions

It returns a non-negative integer: the number of UTF-16 code units in the string. For everyday ASCII/Latin text, that usually matches the visible character count. Empty strings return 0.
Yes. Letters, digits, spaces, tabs, newlines, and punctuation all count. Trim first if you only care about non-whitespace content.
No. Instance length is read-only. Assigning has no effect in sloppy mode and throws TypeError in strict mode. Create a new string with slice or substring instead.
Many emoji use a UTF-16 surrogate pair (two code units). length counts code units, not graphemes. Use [...str].length for code points, or Intl.Segmenter for user-visible grapheme counts.
No. The static String.length is the arity of the String function (1). It is unrelated to the length of string values. Always read .length on a string instance or primitive.
String instance length has been in JavaScript since ES1. It works in every browser and Node.js version with no polyfill.

Next: String at()

Learn relative character access with positive and negative indexes.

at() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful