Get the Last 4 Characters of String
var str = "Hello Welcome"; document.write(str.substr(str.length - 4));
- 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.