JavaScript - Length of a String Last Updated : 17 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report These are the following ways to Find the Length of a String in JavaScript:Using the length PropertyThis approach directly utilizes the built-in length property of strings in JavaScript, which returns the number of characters in the given string. JavaScript let s = "abcd"; console.log(s.length); Using For LoopIn this method, iterates over each characters of the string using a loop and increments a counter variable to keep track of the total number of characters. JavaScript let s = "abcd"; let l = 0; for (let x in s) { l++; } console.log(l); Using RecursionUsing recursion to find a string's length involves a base case returning 0 for an empty string and a recursive case adding 1 to the result of calling the function with the string minus its first character. This process continues until the base case is reached. JavaScript function fun(s) { if (s === "") { return 0; } else { return 1 + fun(s.slice(1)); } } console.log(fun("abcd")); Using the Spread Operator and reduce() MethodAnother approach to determine the length of a given string in JavaScript is by utilizing the spread operator (...) along with the reduce() method. This method is less conventional but demonstrates the flexibility of JavaScript in processing strings and arrays. JavaScript const s = "abcd!"; const l = [...s].reduce(c => c + 1, 0); console.log(l); Comment More infoAdvertise with us Next Article Length of a String A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-Questions Similar Reads JavaScript string.length The string.length is a property in JS which is used to find the length of a given string. The string.length property returns 0 if the string is empty.javascriptlet s1 = 'gfg'; console.log(s1.length); let s2 = ''; console.log(s2.length); let s3 = '#$%A'; console.log(s3.length);Output3 0 4 Syntax:stri 1 min read JavaScript String length Property The JavaScript length property is a fundamental feature used to determine the number of characters in a string. It is essential for various string operations, such as validation, manipulation, and iteration. Understanding how to effectively use the length property can simplify tasks like checking in 2 min read Length of a String Given a string s, the task is to find the length of the string.Examples: Input: s = "abc"Output: 3Input: s = "GeeksforGeeks"Output: 13Input: s = ""Output: 0Using In-built methodsEvery programming language offers a built-in method as well to find the lengthC++// C++ program to find length // of a str 3 min read JavaScript Array length JavaScript array length property is used to set or return the number of elements in an array. JavaScriptlet a = ["js", "html", "gfg"]; console.log(a.length);Output3 Setting the Length of an ArrayThe length property can also be used to set the length of an array. It allows you to truncate or extend t 2 min read How to Get the Length of a String in Bytes in JavaScript ? In JavaScript, determining the length of a string in characters is straightforward using the length property. However, in many cases, particularly when dealing with file sizes, network protocols, or database storage, you might need to know the length of a string in bytes. This is because characters 3 min read JavaScript - How to Count Words of a String? Here are the various methods to count words of a string in JavaScript.1. Using split() MethodThe simplest and most common approach is to use the split() method to divide the string into an array of words and count the length of the array.JavaScriptconst count = (s) => s.trim().split(/\s+/).length 3 min read Like