JavaScript substring first and last character

In this article, we’ll be looking at some ways to quickly get the last character of a string in JavaScript.

Subscribe to Coding Beauty Newsletter

Gain useful insights and advance your web development knowledge with weekly tips and tutorials from Coding Beauty. Over 2,000 developers subscribe.

1. String at() Method

The get the last character of a string, we can call the at() method on the string, passing -1 as an argument. For example, str.at(-1) returns a new string containing the last character of const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 0.

const str = 'Coding Beauty'; const lastChar = str.at(-1); console.log(lastChar); // y

The const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 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() Method

Alternatively, to get the last character of a string, we can call the const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4 method on the string, passing the last character index as an argument. For example, const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 5 returns a new string containing the last character of const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 0.

const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k

The const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 1 const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4 method takes an index and returns the character of the string at that index.

Tip

In JavaScript, arrays use zero-based indexing. This means that the first character has an index of const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 9, and the last character has an index of const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 0.

Note

If we pass an index to const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4 that doesn’t exist on the string, it returns an empty string (const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 2):

const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // ''

3. Bracket Notation ([]) Property Access

We can also use the bracket notation (const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 3) to access the last character of a string. Just like with the const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4 method we use const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 0 as an index to access the last character.

const str = 'book'; const lastCh = str[str.length - 1]; console.log(lastCh); // k

Note

Unlike with const str = 'book'; const lastCh = str.charAt(str.length - 1); console.log(lastCh); // k 4, using the bracket notation to access a character at a non-existent index in the string will return const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 7:

const str = 'book'; const lastCh = str[10]; console.log(lastCh); // undefined

4. String split() and Array pop() Methods

With this method, we call the const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 8 method on the string to get an array of characters, then we call const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 9 on this array to get the last character of the string.

const str = 'book'; const lastCh = str.split('').pop(); console.log(lastCh); // k

We passed an empty string (const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 2) to the const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 8 method to split the string into an array of all its characters.

const str = 'book'; console.log(str.split('')); // [ 'b', 'o', 'o', 'k' ]

The Array const str = 'book'; const lastCh = str.charAt(10); console.log(lastCh); // '' 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 Does

A 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 .

Postingan terbaru

LIHAT SEMUA