Get The Last Character Of A String In JavaScript

There are many ways to get the last character of a String in JavaScript –

1. Using The charAt Method

2. Using The slice Method

3. Using The substr Method

4. Using The substring Method

5. Using The split method

6. Accessing The String Using Array Index

get the last character of a string in javascript

First let’s understand the JavaScript string object. The string object is used to represent and manipulate a sequence of characters. In JavaScript, a string can be created using single or double quotes:

var single = 'Single quotes';

var double = "Double quotes";

You can access the characters in a string by their index position:

var str = "This is a string";

console.log(str.charAt(0)); // prints "T"

console.log(str.charAt(1)); // prints "h"

console.log(str.charAt(2)); // prints "i"

Let’s look at the various methods to get the last character of a String in JavaScript.

1. Using The charAt Method

The charAt() method returns the character at the specified index in a string.

The index of the last character is str.length-1, and the index of the first character is 0.

Therefore, to get the last character from a string, we need to pass the index as str.length-1 to the charAt() method. Here is the code for the same:

var str = "Hello World!";

var lastChar=str.charAt(str.length-1);

console.log(lastChar); //will print "!"

2. Using The slice Method

The slice() method extracts parts of a string and returns the extracted part in a new string.

This method takes 2 parameters, start and end, and returns a new string that is a part of the original string from start to end.

The character at the start index is included, but the character at the end index is not.

slice(-1) means to start at the last character index of the string, which would be the last character.

Here is the code for the same:

var str = "Hello World!";

var lastChar=str.slice(-1);

console.log(lastChar); //will print "!"

3. Using The substr Method

The substr() method extracts parts of a string, beginning at the character specified by start and extending to the end of the string (or up to end, if specified). This method takes 2 parameters – start and length.

Here is the code for the same:

var str = "Hello World!";

var lastChar=str.substr(str.length-1,1);

console.log(lastChar); //will print "!"

4. Using The substring Method

The substring() method extracts the characters from a string between start and end, not including end itself. This method takes 2 parameters – start and end, and returns a new string.

Here is the code for the same:

var str = "Hello World!";

var lastChar=str.substring(str.length-1,str.length);

console.log(lastChar); //will print "!"

5. Using The split method

The split() method splits a string into an array of substrings, and returns the new array.

This method takes 1 parameter – separator, which is used to split the string.

The character at the index returned by separator will be the starting point of the next substring.

If separator is not specified, str is converted to an array of characters.

Here is the code for the same:

var str = "Hello World!";

var lastChar=str.split("")[str.length-1]

console.log(lastChar); //will print "!"

6. Accessing The String Using Array Index

Strings in JavaScript are immutable, which means they cannot be changed. However, we can access individual characters of a string using the array index.

JavaScript strings are zero-indexed: the first character is in position 0, the second in 1, and so on.

To get the last character, we need to access the character at index str.length-1.

Here is the code for the same:

var str = "Hello World!";

var lastChar=str[str.length-1];

console.log(lastChar); //will print "!"

There is one issue with this method though. If you try to access an element at an index that doesn’t exist , you will get an undefined value.

So, it is always better to check if the index exists before accessing it. This is why I like using the charAt method, because it doesn’t give us any undefined values.

This was all about getting the last character of a string in JavaScript. Note that none of these methods alter the original string, they only return a new string with the desired character.

I hope this article was helpful in understanding how to get the last character of a string in JavaScript.

Please feel free to leave your comments and suggestions below. Thank you for reading!

Leave a Reply