How to remove the last character of a string in JavaScript

#javascript

Last updated on Sep 24, 2020 by Suraj Sharma



In this tutorial, you will learn two ways to remove the last character of a JavaScript string



1. Using String.substring() method


You can get the substring except the last character of the actual string by passing (0, string.length-1) as arguments to the substring() method


const string = 'JavaScript';

const newString = string.substring(0, string.length-1)

console.log(newString); // 'JavaScrip'


2. Using String.slice() method


The slice() method accepts negative numbers as its arguments.

So passing (0, -1) to the slice() method, returns a new string without the last character of the actual string.


const string = 'JavaScript';

const newString = string.slice(0, -1) // '-1' is equivalent to 'string.length-1'

console.log(newString); // 'JavaScrip'


Related Articles



Suraj Sharma is a JavaScript Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.