by Sai gowtham

How to get last character of a string in JavaScript

In this tutorial, we will learn two different ways to get the last character of a string in JavaScript.

1. Using charAt() method

By passing string.length-1 as an argument to the charAt() method we can get the last character of a string.

const string = "hello";

const lastCharacter = string.charAt(string.length-1);

console.log(lastCharacter); // "o"

2. Using slice() method

If we pass -1 as an argument to the slice() method we can get the last character of a string.

const string = "hello";

const lastCharacter = string.slice(-1);

console.log(lastCharacter); // "o"

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course