Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JavaScript»2 Methods to Remove Last Character from String in JavaScript

    2 Methods to Remove Last Character from String in JavaScript

    RahulBy RahulJuly 15, 20181 Min ReadUpdated:June 10, 2022

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

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

    Method 1 – Using 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
    3
    4
    5
    6
    7
    8
    // Initialize variable with string
    var str = "Hello TecAdmin!";
     
    // Remove last character of string
    str = str.substring(0, str.length - 1);
     
    // Print result string
    console.log(str)

    Remove last character of string with substring function
    Remove last character of string with substring function

    Method 2 – Using 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
    3
    4
    5
    6
    7
    8
    // Initialize variable with string
    var str = "Hello TecAdmin!";
     
    // Remove last character of string
    str = str.slice(0, -1);
     
    // Print result string
    console.log(str)

    Remove last character of string with javascript slice function
    Remove last character of string with javascript slice function

    Wrap Up

    In this tutorial, you have learned about removing the last character of a sting in JavaScript. We used substring() and slice() functions to alter a string.

    javascript string
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Remove Last Character from String in PHP
    Next Article How to Reset Gnome Desktop on Ubuntu 18.04 Bionic

    Related Posts

    What is difference between var, let and const in JavaScript?

    3 Mins Read

    How to Replace String in JavaScript

    Updated:June 13, 20222 Mins Read

    JavaScript Program to Add Two Numbers

    Updated:June 9, 20222 Mins Read

    Shell Script – Remove Double Quote (“”) from a String

    1 Min Read

    How to Install ReactJS on Ubuntu 20.04

    3 Mins Read

    Remove First Character from String in JavaScript

    1 Min Read

    3 Comments

    1. Daniel Jarocha on November 29, 2021 7:47 pm

      if u want to cut a space char then use .trim()

      Reply
    2. Emrah on January 9, 2021 12:15 pm

      Thanks

      Reply
    3. GhulamRasool on October 24, 2019 12:16 pm

      thanks

      Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How To Install Docker on Ubuntu 22.04
    • How to Install Bower on Ubuntu 22.04 & 20.04
    • How to run “npm start” through Docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.