Javascript String length property

Introduction

Each instance of String contains a single property, length.

string.length

It indicates the number of characters in the string.


let stringValue = "hello world"; 
console.log(stringValue.length);   // "11" 

This example outputs "11", the number of characters in "hello world".

let x = 'CSS';
let empty = '';

console.log(x + ' is ' + x.length + ' code units long');
console.log('The empty string has a length of ' + empty.length);

For a double-byte character, each character is still counted as one.

JavaScript strings consist of 16 bit code units.

For most characters, each 16 bit code unit will correspond to a single character.

The length property indicates how many 16 bit code units occur inside the string:

let message = "abcde"; 
console.log(message.length);  // 5 



PreviousNext

Related