How to Get Last 4 Characters of String in JavaScript

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:

get last 4 characters of string javascript

 

  1. Here, we have created the variable “str” and assigned some values.
  2. If we want to get the last 4 characters, we need to use substr() and length-4.
  3. See the above output, the last 4 characters of the string is displayed successfully.

Leave a Reply