# How to remove the last character from a string in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Oct 25, 2022. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-remove-last-character-from-string

> Learn how to use the slice() and substring() methods to remove the characters from a string in JavaScript.

You can use the `slice()` and `substring()` methods to remove one or more characters from the end of a string in JavaScript.

## Remove the last character from a string

You can use the `slice()` method to remove the last character from a string, passing it `0` and `-1` as parameters:

```javascript
const str = 'JavaScript'

const result = str.slice(0, -1)
console.log(result) // JavaScrip
```

The `slice()` method extracts a part of a string between the start and end indexes, specified as first and second parameters. It returns the extracted part as a new string and does not change the original string.

> Indexes in JavaScript are zero-based. The first character in a string has an index of `0`, and the last has an index of `str.length - 1`. 

We passed `-1` as an end index to the `slice()` method to exclude the last character `t` from the returned string. The negative index of `-1` specifies that we want to skip the ending character and get a new string containing the rest.

Alternatively, you could use the `substring()` method to remove the last character from a string, as shown below:

```javascript
const str = 'JavaScript'

const result = str.substring(0, str.length -1)
console.log(result) // JavaScrip
```

The `substring()` method extracts characters between the start and end indexes from a string and returns the substring.

## Remove the last N characters from a string

You can also use the `slice()` method to remove the last N characters from a string in JavaScript:

```javascript
const str = 'JavaScript'

const removed2 = str.slice(0, -2)
console.log(removed2) // JavaScri

const removed6 = str.slice(0, -6)
console.log(removed6) // Java

const removed9 = str.slice(0, -9)
console.log(removed9) // J
```

The `substring()` method can also be used to remove the last N characters from a string:

```javascript
const str = 'JavaScript'

const removed4 = str.substring(0, str.length - 4)
console.log(removed4) // JavaSc
```
