How to Get Last Character from String in Javascript

To get the last character of a string in JavaScript, use the charAt() method. JavaScript charAt() is a built-in String function that returns the character at a specified index (position) in a string.

Syntax

charAt(index)

Parameters

index –  It takes one parameter called index, between 0 and string.length-1. If we pass an index that is not valid, then it will return the first character from the string. The default value of this method is 0.

Return value

string – It will return a string from the UTF-16 code unit. If the passed index is greater than the string.length-1, it will return an empty string.

Example

const string = "Javascript is beautiful";

// last character
console.log("Last character:", string.charAt(string.length - 1));

Output

Last character: l

We get the last character using the charAt() method above. The string index starts from 0, and that’s why we pass the string.length – 1 argument to the charAt() function.

Using JavaScript slice() method

JavaScript String slice() is a built-in method that extracts a part of a string. It returns the extracted part in a new string and does not change the original string.

Syntax

string.slice(startIndex, endIndex);

Parameters

startIt is the required parameter which is the start position.

end: It is the optional parameter which is the end position.

Example

const string = "Javascript is beautiful";
// last character
console.log("last char : ", string.slice(-1));

Output

last char : l

You can see that we extracted the last character using a string.slice() method.

Using substring() method

JavaScript String.substring() is a built-in method that extracts characters, between two indices (positions), from a string and returns the substring. So, for example, we can use the substring() method to get the last character of the string using the first and last indices.

Syntax

string.substring(start, end)

Parameters

start: It is a required parameter, the start position.

end: It is the optional parameter and the end position.

Example

const string = "Javascript is beautiful";
// last character
console.log("last char : ", string.substring(string.length - 1));

Output

last char : l

Using [] Notation

const string = "Javascript is beautiful";

// last character
console.log("last char : ", string[string.length - 1]);

Output

last char : l

We are using “[]” to access the string elements directly via its index, and it returns the element based on the index. So we need to get the last element of the string. That’s why we pass the string.length – 1 as the argument.

Using spread Operator

The spread operator creates an array of strings and stores them in a variable, and then we access them as array elements.

const string = "Javascript is beautiful";
const arr = [...string];
console.log("last char: ", arr[arr.length - 1]);

Output

last char: l

We converted the string into an array using the […] spread operator and accessed array elements using the box bracket.

Final words

The best way to get the first character from a string in JavaScript is to use the charAt() function. It is the most optimal way to do that. You can use other methods based on your requirements.

That’s it for this tutorial.

Related posts

How to Get Character from String in Javascript

How to Get First Character of String in JavaScript

How to Convert String to Char Code in Javascript

Leave a Comment