How to Remove the Last N Characters from a String in JavaScript?

Estimated read time 1 min read

To remove the last N characters from a string in JavaScript, you can use the slice() method. Here’s an example:

let str = "This is a string";
let n = 5;
let newStr = str.slice(0, -n);

This code removes the last N characters from the string "This is a string" using the slice() method and logs the modified string: "This is a st" where N=5.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply