How to Get Last Character from String in Javascript

Method 1: Using the charAt() function

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

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, so we pass the string.length – 1 argument to the charAt() function.

Method 2: Using the slice() function

JavaScript String slice() is a built-in method that extracts a part of a string. It returns the extracted part in a new string, not changing 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.

Method 3: Using the substring() function

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

Method 4: 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, which 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.

Method 5: 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.

That’s it.

Leave a Comment