Open In App

How to get the last character of a string in JavaScript ?

Last Updated : 29 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how can we get the last character of a string in JavaScript. we have given a string of size len, the task is to get the last character of a string using JavaScript.

There are several methods to solve this problem:

Method 1: Using charAt() Method

In this approach, we will count the number of characters in a given string by using the str.length property. Since the indexing starts from 0 use str.charAt(str.length-1) to get the last character of the string. 

Example: This example implements the above approach.

Javascript




// Input String
let str = "GeeksforGeeks";
 
// Getting last character using char at
let res = str.charAt(str.length - 1);
 
// Display output
console.log(res);


Output

s

Method 2: Using str.slice() Method

In this approach, we will use the string.slice() function and it will return the last character of the string.

Example: This example uses the slice() function to get the last character of the string. 

Javascript




// Input string
let str = "GeeksforGeeks";
 
// Getting last character using slice method
let res = str.slice(-1);
 
// Display output
console.log(res);


Output

s

Method 3: Using str.substr() Method

In this approach, we will use the str.substr method and it returns the specified number of characters from the specified index from the given string.

Example: This example uses str.substr to print the last character of the string.

Javascript




// Input string
let str = "GeeksforGeeks";
 
// Getting last character using substr method
let res = str.substr(-1);
 
// Display Output
console.log(res);


Output

s

Method 4:Using JavaScript str.length for index

Using string indexing, we can use string indexing to access the element of the array and use the length property of the string to access the last element index 

Example: This example uses string indexing to print the last character of the string.

Javascript




// Input string
let str = "GeeksforGeeks";
 
// Getting last character using str.length
let res = str[str.length-1];
 
// Display output
console.log(res);


Output

s

Method 5: Using String.at() method

In this method, we can pass -1 for getting the last character of the string.

Syntax:

str.at(-1)

Example: In this example, we will use str.at() for getting the last character.

Javascript




// Input string
let str = "GeeksforGeeks";
 
// Getting last character using string.at method
let res = str.at(-1);
 
// Display output
console.log(res);


Output

s

Method 6: Using String.match() method

In this method, we will use regular expression for getting the last character of the given string.

Example: In this example, we will use str.match() for getting the last character.

Javascript




// Input string
let str = "GeeksforGeeks";
 
// Getting last charcter using match method
let res = str.match(/.$/g);
 
// Display output
console.log(res);


Output

[ 's' ]

Time complexity: O(N), Where N is the length of the string. 

Auxiliary complexity: O(1), Because no extra space is used. 



Previous Article
Next Article

Similar Reads

How to get the last character of a string in PHP ?
In this article, we will find the last character of a string in PHP. The last character can be found using the following methods. Using array() methodUsing substr() method Using array() Method: In this method, we will find the length of the string, then print the value of (length-1). For example, if the string is "Akshit" Its length is 6, in zero-i
2 min read
JavaScript Program to Remove Last Character from the String
In this article, we will learn how to remove the last character from the string in JavaScript. The string is used to represent the sequence of characters. Now, we will remove the last character from this string. Example: Input : Geeks for geeks Output : Geeks for geek Input : Geeksforgeeks Output : Geeksforgeek Below are the following approaches th
3 min read
How to Find the First and Last Character of a String in JavaScript ?
In this article, we are going to learn about finding the first and last character of a string in JavaScript. The first character of a string is its initial symbol, while the last character is the final symbol at the end of the string. Strings are sequences of characters. Here we have some common methods to find the first and last character of a str
3 min read
How to get value of a string after last slash in JavaScript?
The task is to get the string after a specific character('/'). Here are a few of the most used techniques discussed. We are going to use JavaScript. Below are the approaches used to get the value of a string after the last slash in JavaScript: Table of Content Using .split() method and .length propertyUsing .lastIndexOf(str) method and .substring()
2 min read
How to Get the Last N Characters of a String in JavaScript
We will learn how to get the last N characters of a string in JavaScript. We have given a string and we need to get the "n" character from the last of the string. There are various methods for finding the "n" character from the last of the string. Below are the following methods through which we get the last N characters of a string in JavaScript:
3 min read
JavaScript Program to Get a Non-Repeating Character From the Given String
In JavaScript, we can find the non-repeating character from the input string by identifying the characters that occur only once in the string. There are several approaches in JavaScript to get a non-repeating character from the given string which are as follows: Table of Content Using indexOf and lastIndexOf methodsUsing Set and Array.from()Using M
3 min read
How to Get Character Array from String in JavaScript ?
The string is defined as a sequence of characters. In this article, we will learn how to get a character array from a string in JavaScript. Table of Content JavaScript String split() FunctionJavaScript Array.from() FunctionJavaScript Spread OperatorUsing for ... of LoopUsing Array.prototype.map()JavaScript String split() FunctionThe str.split() fun
3 min read
How to replace characters except last with the specified mask character in JavaScript ?
In this article, we have given a number and the task is to replace the characters except for the last character with a specified mask character in JavaScript. These are the following methods: Using Naive approachUsing slice() and replace methodUsing reduce() methodExample: Let's take a number 12345678 and the masking character is * and the number o
4 min read
PHP Program to Remove Last Character from the String
Given a String, the task is to remove the last character from a string in PHP. This operation is common in text processing and manipulation. Examples: Input: str = "Hello Geeks!"Output: Hello GeeksInput: str = "Welcome"Output: WelcomThese are the following approaches: Table of Content Using substr() FunctionUsing rtrim() FunctionUsing String Manipu
2 min read
How to get the first and last date of current month using JavaScript ?
Given a month and the task is to determine the first and last day of that month in proper format using JavaScript. JavaScript getDate() Method: This method returns the day of the month (from 1 to 31) for the defined date. Syntax: Date.getDate() Parameters: This method does not accept any parameters. Return value: It returns a number from 1 to 31, r
3 min read