Open In App

Can we Change the Length of a JS String using Length Property?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The length property of a string returns the number of characters in the string, including spaces, punctuation, and special characters. It is a read-only property, which means that you cannot assign a new value to length directly to change the string's size. The given below example shows that you can change the length of the given array by the use of the length property but it does not have any effect on string.

JavaScript
let a = ["js", "css", "html"];
a.length = 2;  // Changes length of array
console.log(a);

Output
[ 'js', 'css' ]

The length property of a string in JavaScript is read-only, meaning you cannot assign a new value to it to change the string’s length. JavaScript strings are immutable, so they cannot be modified after they are created. Any operation that alters a string, such as slicing, concatenating, or trimming, creates a new string rather than modifying the original one.

JavaScript
let s = "geeksforgeeks"
s.length = 3;  // Has no effect on string
console.log(s);

Output
geeksforgeeks

Article Tags :

Similar Reads