• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Remove the First and Last Character From a String in JavaScript

Remove the First and Last Character From a String in JavaScript

May 14, 2022 Leave a Comment

We can remove the first and last character from a string using JavaScript easily with the use of the String slice() method.

var newString = oldString.slice(1,-1);

The code above will return the original string minus the first and last characters.


Let’s say we have the following JavaScript:

var someString = "This is some example text";
var newString = someString.slice(1,-1);

In the above code, newString will have the following value after applying the substring method to the original string:

his is some example tex

We can also remove JUST the first character from a string using the substring() method and remove JUST the last character from a string using the slice() method.

Let’s take a look at another simple example.

Removing the First and Last Characters From a String in JavaScript with a Click

In this simple example, we will have a really long string to start out. We will then provide a button to let the user remove the first and last characters from that string as many times as they want.

Here is the HTML setup:

<div id="div1">
  <div id="updatedString"></div>
  <div id="click-me" onclick="removeFirstLastCharacters()">Remove first and last characters</div>
</div>

First, we will populate the div #updatedString with a long string we will make up.

We will then add an onclick event to our #click-me div that will run a function we will create called removeFirstLastCharacters(). Our function will remove the first and last characters of the string using the slice() method.

We will finally update the #updatedString div using the textContent property with the new string.

Here is the JavaScript code:

//Our string
var startingString = "This is a long string that we can remove the first character from as many times as we want by pressing the button below.";
  
//We will populate the div #updatedString with our string
document.getElementById("updatedString").textContent = startingString;

function removeFirstLastCharacters(){

  //We remove the first and last character of the string
  startingString = startingString.slice(1,-1);

  //We will then update the div #updatedString with our new string
  document.getElementById("updatedString").textContent = startingString;

}

The final code and output for removing the first and last character from a string using JavaScript is below:

Code Output:

Remove first and last characters

Full Code:

<div id="div1">
  <div id="updatedString"></div>
  <div id="click-me" onclick="removeFirstLastCharacters()">Remove first and last characters</div>
</div>

<script>

var startingString = "This is a long string that we can remove the first character from as many times as we want by pressing the button below.";

document.getElementById("updatedString").textContent = startingString;

function removeFirstLastCharacters(){
  startingString = startingString.slice(1,-1);
  document.getElementById("updatedString").textContent = startingString;
}

</script>

Hopefully this article has been helpful for you to learn how to remove the first and last character from a string in JavaScript.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Get a Random Number Between 1 and 20
  • 2.  Remove Word From String In JavaScript
  • 3.  JavaScript Infinite Loop
  • 4.  Using JavaScript to Replace a Space with an Underscore
  • 5.  Math abs JavaScript – How to get the absolute value of a number using JavaScript
  • 6.  How to Return Multiple Values in JavaScript
  • 7.  How to Remove All Numbers From String in JavaScript
  • 8.  Using JavaScript to Get the Length of Array
  • 9.  Using JavaScript to Set Visibility
  • 10.  Using JavaScript to Convert String to Boolean

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy