JavaScript substring() Method

JavaScript substring() Method returns a part of a string from start position to end position.

Positions are index values of characters in a string. Remember that element placed at the end position is not included in the return string.

Let's have a variable named string1, substring method will return a new string with characters from 0 index position to 3rd index position.

let string1 = 'HowToCodeSchool';
let string2 = string1.substring(0, 4);
document.getElementById('output').innerHTML = 'Output: ' + string2;

Output: HowT

If only one position is defined then all characters from that position are returned as a string. Remember that we can't use negative numbers in substring() method unlike slice() method.

let string1 = 'HowToCodeSchool';
let string2 = string1.substring(4);
document.getElementById('output').innerHTML = 'Output: ' + string2;

Output: oCodeSchool

Video Tutorial

Watch video tutorial on JavaScript substring() Method.