JavaScript Code

JavaScript String substr()


String substr()

substr(start)           #if no length, then till the end
substr(start, length)
  • start is a number
  • length is a number

substr() method finds the substring of given length from the given start.

Examples

1. substr(start)

let name = 'abcdefghijklmn';
let output = name.substr(6);
console.log(output);

2. substr(start) with start as negative number. the index is considered from end towards start.

let name = 'abcdefghijklmn';
let output = name.substr(-6);
console.log(output);

3. substr(start, end)

let name = 'abcdefghijklmn';
let output = name.substr(6, 5);
console.log(output);

Copyright @2022