Get the Last 4 Characters of String
To get the last 4 characters of a string in javascript, we need to use substring () function and length() function. Let’s see the example code.
Ex:
var str = "Hello Welcome";
document.write(str.substr(str.length - 4));
Output:
- Here, we have created the variable “str” and assigned some values.
- If we want to get the last 4 characters, we need to use substr() and length-4.
- See the above output, the last 4 characters of the string is displayed successfully.