JS get last character of a string: Given a string, the task is to print the last character of the given string using JavaScript.
Get the Last Character of a String Using JavaScript
Method #1: Using charAt() Function
To get the last character of a string, use the string’s charAt() method, passing it the last index as an argument. For example, gvn_str .charAt(str.length – 1) returns a new string with the string’s last character.
The charAt()
function returns the character present at the specified index.
Here gvn_str.length is used to find the total length of the given string.
Approach:
- Give the string as static input and store it in a variable.
- Pass the last index of the string(length of the string – 1) to the charAt() function and apply it to the given string to get the last character of the given string.
- Print the last character of the given string.
- The Exit of the Program.
Below is the implementation:
// Give the string as static input and store it in a variable. const gvn_str = 'hello btechgeeks'; // Pass the last index of the string(length of the string - 1) to the // charAt() function and apply it on the given string to get the last // character of the given string const last_char = gvn_str.charAt(gvn_str.length - 1); // Print the last character of the given string console.log(" The last character of the given string = ",last_char);
Output:
The last character of the given string = s
NOTE:
JavaScript indexes are zero-based. Since the first character in the string has an index of 0, the last character has an index of gvn_str.length - 1.
Passing index on an Empty String
The charAt method returns an empty string if an index is passed that does not exist on the string.
// Give the empty string as static input and store it in a variable. const gvn_str = ''; // Pass the last index of the string(length of the string - 1) to the // charAt() function and apply it on the given string to get the last // character of the given string const last_char = gvn_str.charAt(gvn_str.length - 1); // Print the last character of the given string console.log(last_char);
Output:
Method #2: Using slice() Function
Approach:
- Give the string as static input and store it in a variable.
- Pass the last index value using negative indexing -1 to the slice() function and apply it on the given string to get the last character of the given string.
- Print the last character of the given string.
- The Exit of the Program.
Below is the implementation:
// Give the string as static input and store it in a variable. const gvn_str = 'good morning'; // Pass the last index value using negative indexing -1 to the // slice() function and apply it on the given string to get the // last character of the given string const last_char = gvn_str.slice(-1) // Print the last character of the given string console.log(" The last character of the given string = ",last_char);
Output:
The last character of the given string = g
Method #3: Using Bracket Notation to Access the Index on the String.
To get the last character of a string, use bracket notation to access the string at the last index. For example, gvn_str[gvn_str.length – 1] returns the string’s last character.
Approach:
- Give the empty string as static input and store it in a variable.
- Use the bracket notation to the last character of a given string and store it in another variable.
- Here gvn_str.length – 1 represents the last index
- Print the last character of the given string.
- The Exit of the Program.
Below is the implementation:
// Give the empty string as static input and store it in a variable. const gvn_str = 'hello btechgeeks'; // Use the bracket notation to the last character of a given string // Here gvn_str.length - 1 represents the last index const last_char = gvn_str[gvn_str.length - 1]; // Print the last character of the given string console.log(" The last character of the given string = ",last_char);
Output:
The last character of the given string = s