Question #570   Submitted by Answiki on 09/20/2021 at 07:47:25 PM UTC

How to get the length of a string in JavaScript?

Answer   Submitted by Answiki on 09/20/2021 at 07:59:28 PM UTC

The property string.length returns the number of bytes in a string, i.e. the length.


Example :

str ="Hello world";
len = str.length;  // len = 11, the length of str


Warning : string.length returns the number of bytes, not the number of characters. Some characters (UTF-8, UNICODE ...) are encoded on several bytes :

str ="????";
console.log (str.length); // Display 2

8 events in history
Answer by Answiki on 09/20/2021 at 07:59:28 PM

The property string.length returns the number of bytes in a string, i.e. the length.


Example :

str ="Hello world";
len = str.length;  // len = 11, the length of str


Warning : string.length returns the number of bytes, not the number of characters. Some characters (UTF-8, UNICODE ...) are encoded on several bytes :

str ="????";
console.log (str.length); // Display 2

Answer by Answiki on 09/20/2021 at 07:58:59 PM

The property string.length returns the number of bytes in a string, i.e. the length.


Example :

str ="Hello world";
len = str.length;  // len = 11, the length of str


Warning : string.length returns the number of bytes, not the number of characters. Some characters (UTF-8, UNICODE ...) are encoded on several bytes :

str ="????";
console.log (str.length); // Display 2

Question by Answiki 09/20/2021 at 07:58:22 PM
How to get the length of a string in JS?
Question by Answiki 09/20/2021 at 07:47:25 PM
How to get the length of a string in JavaScript?
Question by Answiki 09/20/2021 at 07:47:15 PM
How to get the length of strings in JavaScript?
Question by Answiki 09/20/2021 at 07:47:01 PM
In JavaScript, how to get the length of strings?
Answer by Answiki on 09/20/2021 at 07:46:51 PM

The property string.length returns the number of bytes in a string.


Example :

str ="Hello world";
len = str.length;  // len = 11, the length of str


Warning : string.length returns the number of bytes, not the number of characters. Some characters (UTF-8, UNICODE ...) are encoded on several bytes :

str ="????";
console.log (str.length); // Display 2

Question by Answiki 09/20/2021 at 07:39:33 PM
In JavaScript, how to get the length of a string?
# ID Query URL Count

Icons proudly provided by Friconix.