How to Find an Exact String With JavaScript

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:

  1. The starting point (0)
  2. 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.


Has this been helpful to you?

You can support my work by sharing this article with others, or perhaps buy me a cup of coffee 😊

Kofi

Share & Discuss on