To get the length of a given string in JavaScript, you can use the length
property of the string object.
In this example,
str1
with a string value of "Hello World"
.length
property of the string object str1
and store the returned value in the length1
variable.length
property returns the length of this string.length1
to the console using console.log()
.// Length of "Hello World" String
let str1 = "Hello World";
let length1 = str1.length;
console.log(`Length of given string is: ${length1}`);
Length of given string is: 11
In this example,
str2
with a string value of "apple"
.length
property of the string object str2
and store the returned value in the length2
variable.length
property returns the length of this string.length2
to the console using console.log()
.// Length of "apple" String
let str2 = "apple";
let length2 = str2.length;
console.log(`Length of given string is: ${length2}`);
Length of given string is: 5
In this tutorial, we learned How to get Length of String in JavaScript language with well detailed examples.