length

Summary

The length of a string.

Property of String
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript 1st Edition

Description

This property returns the number of code units in the string. UTF-16, the string format used by JavaScript, uses a single 16-bit code unit to represent the most common characters, but needs to use two code units for less commonly-used characters, so it's possible for the value returned by length to not match the actual number of characters in the string.

For an empty string, length is 0.

Examples

var x = "Netscape";
var empty = "";

console.log("Netspace is " + x.length + " code units long");
console.log("The empty string is has a length of " + empty.length); /* should be 0 */

See also

Tags (1)

Contributors to this page: evilpie, Sheppy, ziyunfei, ethertank, Nickolay, Mgjbot, Maian, Dria, jstrimpel
Last updated by: ethertank,
Last reviewed by: ethertank,