All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Remove Last Character From String In JavaScript

Last Updated : Mar 11, 2024

How To Remove Last Character From String In JavaScript

In this tutorial we will show you the solution of how to remove last character from string in JavaScript, mainly there are many methods with the help of which we are able to remove last character.

Removing last character is very important topic and complex topic. Let us understand how to done this with help of codes.

Step By Step Guide On How To Remove Last Character From String In JavaScript :-

As, we already said that there are many method with help of which we are able to remove last character from string using JavaScript.

First one is using slice(), second is substring() and last one is substr(). In this article, we understand our first method that is slice(). So, let us understand slice() using codes.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
</head>
<body>
 <script type="text/javascript">
  const str = 'talkerscodes'
  const modifiedText = str.slice(0, -1)
  console.log(modifiedText);
 </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. Here, inside body we use JavaScript under script tag. Script tag is always paired tag and we use this tag inside body of html.
  6. Under script tag, first we create a constant with name str and assigns some value to it. One thing to note here that you can create a variable with any method.
  7. It may be created with let or var and may be from const. here str is used to store variable.
  8. In next step, we use str.slice(0, -1) function, in this function we only have to give index starting and ending value of stored string.
  9. 0 is our starting index and we use -1 because we want to remove last character. So, to remove one character we use -1 here.
  10. And we store this new string after slicing in our new variable named as modifiedText. And to show this we print the stored string with help of console. We hope you understand codes properly.
  11. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to remove last character from string in JavaScript.

I hope this tutorial on how to remove last character from string in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪