Avatar ·

How to remove the last character from a string in JavaScript? Methods slice, substring and replace

Hello everyone! I'm a beginner in JavaScript and I've encountered a problem.

\n\n

I have a string, for example: "Hello, world!". I need to remove the last character (the exclamation mark) from it to get "Hello, world". I know that strings in JS are immutable, but you can create a new string without the last character.

\n\n

I tried using trim(), but it only removes whitespace from the beginning and end of the string. Then I found the slice() method, but I didn't fully understand how to work with it using negative indices. I also saw examples online with substring() and replace(), but they sometimes behave strangely.

\n\n

Please tell me, what is the most reliable and performant way to remove the last character from a string? And explain