• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

2 Methods to Remove Last Character from String in JavaScript

Written by Rahul, Updated on July 18, 2018

Question: How do I remove last character from a string in JavaScript or Node.js script?

This tutorial describes 2 methods to remove last character from a string in JavaScript programming language. You can use any one of the following methods as per the requirements.

Method 1 – substring function

Use the substring() function to remove the last character from a string in JavaScript. This function returns the part of the string between the start and end indexes, or to the end of the string.

Syntax:

1
str.substring(0, str.length - 1);

Example:

1
2
var str= "Hello TecAdmin!";
var newStr = str.substring(0, str.length - 1);

Method 2 – slice function

Use the slice function to remove the last character from any string in JavaScript. This function extracts a part of any string and return as new string. When you can store in a variable.

Syntax:

1
str.slice(0, -1);

Example:

1
2
var str = "Hello TecAdmin!";
var newStr = str.slice(0, -1);

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

2 Comments

  1. Avatar Emrah Reply
    January 9, 2021 at 12:15 pm

    Thanks

  2. Avatar GhulamRasool Reply
    October 24, 2019 at 12:16 pm

    thanks

Leave a Reply Cancel reply

Popular Posts

  • How To Install Python 3.9 on Ubuntu 20.04 5
  • How To Install Python 3.9 on Ubuntu 18.04 0
  • How to Use AppImage on Linux (Beginner Guide) 2
  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy