• 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 / How to Get the First Character of a String in JavaScript

How to Get the First Character of a String in JavaScript

March 10, 2022 Leave a Comment

We can get the first character of a string in JavaScript easily using the String substring() method.

var firstCharacter = someString.substring(0,1);

In the code above, firstCharacter will be the first character of the string.


Let’s say we have the following JavaScript:

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

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

We can also get the first character of a string using the String slice() method.

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

This code will provide the same results as the substring method did above.

Let’s take a look at another simple example.

Using JavaScript to Get and Remove the First Character of a String 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 character, or get the first character from that string as many times as they want.

Here is the HTML setup:

<div id="div1">
  <div id="updatedString"></div>
  <div id="click-me1" class="click-me" onclick="getFirstCharacter()">Get first character</div>
  <div id="click-me2" class="click-me" onclick="removeFirstCharacter()">Remove first character</div>
  <div id="results"></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-me1 div that will run a function we will create called getFirstCharacter(). Our function will get the first character of the string using the substring() method, and display it below using the innerHTML method.

We will also add an onclick event to our #click-me2 div that will run a function we will create called removeFirstCharacter(). Our function will remove the first character of the string using the substring() method. We will 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 getFirstCharacter(){
  
  //Get the first character of the string
  var firstCharacter = startingString.substring(0,1);
  
  //Display the character to the user
  document.getElementById("results").innerHTML = "The first character is: <b>" + firstCharacter + "</b>";
  
};

function removeFirstCharacter(){

  //We remove the first character of the string
  startingString = startingString.substring(1);

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

};

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

Code Output:

Get first character
Remove first character

Full Code:

<div id="div1">
  <div id="updatedString"></div>
  <div id="click-me1" class="click-me" onclick="getFirstCharacter()">Get first character</div>
  <div id="click-me2" class="click-me" onclick="removeFirstCharacter()">Remove first character</div>
  <div id="results"></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 getFirstCharacter(){
  var firstCharacter = startingString.substring(0,1);
  document.getElementById("results").innerHTML = "The first character is: <b>" + firstCharacter + "</b>";
};

function removeFirstCharacter(){
  startingString = startingString.substring(1);
  document.getElementById("updatedString").textContent = startingString;

};

</script>

Hopefully this article has been useful for you to learn how to get the first character of a string in JavaScript.

Other Articles You'll Also Like:

  • 1.  setInterval JavaScript – How to Repeat Code in Set Time Intervals
  • 2.  JavaScript String startsWith Method
  • 3.  Using JavaScript to Get the Current URL
  • 4.  Insert Character Into String In JavaScript
  • 5.  Remove String From Array in JavaScript
  • 6.  Remove Undefined Values From an Array in JavaScript
  • 7.  JavaScript Random Color – How to Generate a Random Color In JavaScript
  • 8.  Sort List of Divs with Data Attribute with Radio Buttons Using Javascript
  • 9.  Remove Word From String In JavaScript
  • 10.  Remove Special Characters From String in JavaScript

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

x