How to Get Last Character of String in JavaScript
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:
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”:
Then, call the charAt() method by passing the last index of the string and store the returned character in variable “getLastCh”:
Finally, print the last character of a string returned by a “charAt()” method using “console.log()” method:
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:
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:
Finally, print the last character on the console:
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:
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:
Print the resultant value using the “console.log()” method:
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:
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:
Finally, print the last character of the string using “console.log()” method:
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:
Example
Now, we will use the “bracket notation” to get the string’s last character:
Lastly, print the returned last character of the string on the console:
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.
Source: linuxhint.com