In this article, we’ll be looking at some ways to quickly get the last character of a string in JavaScript. Show
Subscribe to Coding Beauty NewsletterGain useful insights and advance your web development knowledge with weekly tips and tutorials from Coding Beauty. Over 2,000 developers subscribe. 1. String at() MethodThe get the last character of a string, we can call the 0.
The 1 at() method returns the character of a string at the specified index. When negative integers are passed to at() , it counts back from the last string character.2. String charAt() MethodAlternatively, to get the last character of a string, we can call the 4 method on the string, passing the last character index as an argument. For example, 5 returns a new string containing the last character of 0.
The 1 4 method takes an index and returns the character of the string at that index.TipIn JavaScript, arrays use zero-based indexing. This means that the first character has an index of 9, and the last character has an index of 0.NoteIf we pass an index to 4 that doesn’t exist on the string, it returns an empty string ( 2):
3. Bracket Notation ([]) Property AccessWe can also use the bracket notation ( 3) to access the last character of a string. Just like with the 4 method we use 0 as an index to access the last character.
NoteUnlike with 4, using the bracket notation to access a character at a non-existent index in the string will return 7:
4. String split() and Array pop() MethodsWith this method, we call the 8 method on the string to get an array of characters, then we call 9 on this array to get the last character of the string.
We passed an empty string ( 2) to the 8 method to split the string into an array of all its characters.
The Array 9 method removes the last element from an array and returns that element. We call it on the array of characters to get the last character.Every Crazy Thing JavaScript DoesA captivating guide to the subtle caveats and lesser-known parts of JavaScript. Sign up and receive a free copy immediately. Ayibatari Ibaba Ayibatari Ibaba is a software developer with years of experience building websites and apps. He has written extensively on a wide range of programming topics and has created dozens of apps and open-source libraries. How to get first and last character of string in JavaScript?The index of the first character is 0 , and the index of the last character—in a string called stringName is stringName. length - 1 . If the index you supply is out of this range, JavaScript returns an empty string. If no index is provided to charAt() , the default is 0 .
How do you find the first and last character of a string?The idea is to use charAt() method of String class to find the first and last character in a string. The charAt() method accepts a parameter as an index of the character to be returned. The first character in a string is present at index zero and the last character in a string is present at index length of string-1.
How do you trim first and last characters?The idea is to use the substring() method of String class to remove first and the last character of a string. The substring(int beginIndex, int endIndex) method accepts two parameters, first is starting index, and the second is ending index.
How to substring last two characters in JavaScript?To get the last two characters of a string in JavaScript, call the slice() method on the string, passing -2 as an argument. For example, str. slice(-2) returns a new string containing the last two characters of str .
|