- Instant help with your JavaScript coding problems

Get string length in JavaScript

Question:
How to get string length in JavaScript?
Answer:
let text = 'This is a text to test';

let length = text.length

console.log('Length is: ' + length + ' characters');
Description:

The length property of a String object contains the length of the string, in UTF-16 code units. length is a read-only data property of string instances.

For an empty string, length is 0.

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.

Share "How to get string length in JavaScript?"