JavaScript string.length (original) (raw)

Last Updated : 16 Nov, 2024

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.

javascript `

let s1 = 'gfg'; console.log(s1.length);

let s2 = ''; console.log(s2.length);

let s3 = '#$%A'; console.log(s3.length);

`

**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.

JavaScript `

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