1var string = "This is a string";
2var a = string.substring(2, 5); //Start at index 2 and end at index 5
3console.log(a); //output "is "
4var b = string.substr(2, 5); //Start at index 2 and stop after 5 characters
5console.log(b); //output "is is"
1let demo = 'This is a normal sentence';
2
3// substr
4// The substr method takes two parameters
5// param 1 - the starting index to copy from a string
6// param 2 - the number of characters that must be copied from the starting index
7
8let newDemo = demo.substr(3, 5);
9// newDemo would be 's is '
10
11// substring
12// The substring method takes 1 mandatory and 1 optional parameter
13// param 1 - the starting index to copy from a string
14// param 2 - (optional) the ending index that will be copied until from the starting index.
15 // - it will not include the ending index position
16 // - if this param is left blank. it defualts to the rest of the string
17
18let newDemo = demo.substring(0, 3);
19// newDemo would be 'Thi'
20
21let newDemo = demo.substring(3);
22// newDemo would be 's is a normal sentence'
23
24