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.
let s1 = 'gfg';
console.log(s1.length);
let s2 = '';
console.log(s2.length);
let s3 = '#$%A';
console.log(s3.length);
Output
3 0 4
Syntax:
string.length
Return Values: It returns the length of the given string.
In JavaScript arrays also have length property to find length of an array. But unlike arrays, we cannot change length of string using length properly because strings are immutable in JS.
let a = ["js", "css", "html"];
a.length = 2; // Changes length of array
console.log(a);
let s = "geeksforgeeks"
s.length = 3; // Has no effect on string
console.log(s);
Output
[ 'js', 'css' ] geeksforgeeks
Recommended Links