by Sai gowtham

How to find length of a string in JavaScript

Learn, how to find the length of a string in JavaScript

Using the string.length

In JavaScript, we can use the string.length property to get the length of a given string.

Here is an example:

const str = "ocean";

const length = str.length;

console.log(length); // 5

The string.length property returns the total number of characters in a string.

Note: string.length property is read-only.

Similarly, we can also use the split() method in JavaScript to find the length of a string.

const str = "opera";

const length = str.split("").length;

console.log(length); // 5

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course