| by Arround The Web | No comments

How to Get Last Character of String in JavaScript

A string comprises several characters, some of which may be special characters, numbers, or letters. As a result, a string’s last character can either be a letter, a number, or a special character, so if you want to know about the last character of a string, use the JavaScript predefined methods.

This article will provide multiple ways to get the string’s last character.

How to Get Last Character of String in JavaScript?

To get the string’s last character in JavaScript, use the following methods:

  • charAt() method
  • at() method
  • substr() method
  • slice() method
  • Bracket notation

Let’s discuss the working of all of these methods one by one!

Method 1: Get Last Character of String Using charAt() Method

For getting the string’s last character, use the “charAt()” method. It takes the index of the last character as a parameter and returns the character as an output at that index. The “string.length – 1” is passed as an argument because the index of the string starts at 0, and the last index is the string.length – 1.

Syntax
Follow the below-given syntax for using the charAt() method:

string.charAt(string.length - 1);

Here, the “string.length – 1” refers to the last index of the string.

Example
In this example, firstly, we will create a variable named “str” that stores a string “LinuxHint”:

var str = "LinuxHint";

Then, call the charAt() method by passing the last index of the string and store the returned character in variable “getLastCh”:

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

Finally, print the last character of a string returned by a “charAt()” method using “console.log()” method:

console.log(getLastCh);

The output displayed “t” as the last character of the “LinuxHint” string:

Let’s move to the second method.

Method 2: Get Last Character of String Using at() Method

JavaScript predefined “at()” method is also used to get the string’s last character. It takes “-1” as an argument which is the position of the last character of a string, and it returns the character at that position.

Syntax
Use the given syntax for at() method:

string.at(-1);

Here, “-1” indicates the last index of the string.

Example
We will use the same string “str” created in the above example and then call the “at()” method by passing the index of the last character:

var getLastCh = str.at(-1);

Finally, print the last character on the console:

console.log(getLastCh);

As you can see, we have successfully fetched the last character “t” of a string “LinuxHint”:

Let’s see another method for getting the last character of a string.

Method 3: Get Last Character of String Using substr() Method

To get the string’s last character, use the JavaScript predefined “substr()” method. It takes the index of the last character as a parameter and gives the character at that index as a substring.

Syntax
Follow the below-provided syntax for the substr() method:

string.substr(string.length - 1);

The “string.length – 1” is the last index of the string.

Example
Here, we will use the same string created in the previous example and call the “substr()” method:

var getLastCh = str.substr(str.length - 1);

Print the resultant value using the “console.log()” method:

console.log(getLastCh);

Output

Let’s see one more method to do the same task.

Method 4: Get Last Character of String Using slice() Method

You can also use the “slice()” method for getting the string’s last character in JavaScript. It gives the extracted part in a new string without altering the existing string. The slice() method accepts a string as input and returns a substring; if you start the slicing at -1, the last character in the string will be returned.

Syntax
For the slice() method, use the given syntax:

string.slice(-1);

Here, “-1” indicates the last index of the string.

Example
In this example, we will use the previously created string and invoke the “slice()” method to get the last character from a string:

var getLastCh = str.slice(-1);

Finally, print the last character of the string using “console.log()” method:

console.log(getLastCh);

The output returns the last index “t” of the string “LinuxHint”:

Let’s move to the next method.

Method 5: Get Last Character of String Using Bracket Notation

The bracket notation “[ ]” is another method for getting the string’s last character. In order to get the string’s last character, you can utilize the “str.length – 1”.

Syntax
Follow the below-mentioned syntax for using bracket notation:

string[string.length - 1];

Example
Now, we will use the “bracket notation” to get the string’s last character:

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

Lastly, print the returned last character of the string on the console:

console.log(getLastCh);

Output

We have gathered all the methods for getting the last character from a string.

Conclusion

To get the last character from a string, you can use the JavaScript built-in methods, such as the charAt() method, at() method, substr() method, slice() method, or Bracket notation. However, the at() and the charAt() methods are the common methods utilized to get the last character. This article provided multiple ways to get the last character from a string with detailed examples.

Share Button

Source: linuxhint.com

Leave a Reply