The simplest way to remove the last character from a string with JavaScript is to use the slice()
method.
To do that, we’ll need to add two parameters inside the slice()
method:
- The starting point (0)
- The number of items to remove (1)
Here’s an example where we correct a misspelled company name:
const companyName = "Netflixx"
const fixCompanyName = companyName.slice(0, -1)
console.log(fixCompanyName)
// output: 'Netflix'
Note: the splice()
method does not modify the original variable value Netflixx
, that one still exists. We created a new variable (fixCompanyName
) that we assign the correct spelling to — and then reference that variable whenever we need to access the company name in a project.