How to use str.slice() Method In Javascript
In this approach, we will use the string.slice() function and it will return the last character of the string.
Example: This example uses the slice() function to get the last character of the string.
// Input string
let str = "w3wiki";
// Getting last character using slice method
let res = str.slice(-1);
// Display output
console.log(res);
Output
s
How to get the last character of a string in JavaScript ?
We are going to learn how can we get the last character of a string in JavaScript. we have given a string of size len, the task is to get the last character of a string using JavaScript.
There are several methods to solve this problem:
Table of Content
- Method 1: Using charAt() Method
- Method 2: Using str.slice() Method
- Method 3: Using str.substr() Method
- Method 4:Using JavaScript str.length for index
- Method 5: Using String.at() method
- Method 6: Using String.match() method
- Method 7: Using Array.prototype.pop() Method