To remove the last N characters of a string in JavaScript, you can use the slice method with the range from 0 to length - N.
In this example,
str1
with a value of "Hello World"
.let str1 = 'Hello World';
let modifiedStr = str1.slice(0, -3);
console.log('Modified string is:', modifiedStr);
Modified string is: Hello Wo
In this example,
str2
with a value of "apple"
.let str2 = 'apple';
let modifiedStr = str2.slice(0, -2);
console.log('Modified string is:', modifiedStr);
Modified string is: app
In this tutorial, we learned How to Remove last N Characters in a String in JavaScript language with well detailed examples.