JavaScript Code

JavaScript String startsWith()


String startsWith()

str.startsWith(search, start)
  • str is a string
  • search is a string
  • start is an integer, and optional

startsWith() method returns true if the string str starts with the specified search string, or else it returns false. If start is provided, only the part of the string str from the specified start index position is considered for checking.

Examples

1. check if string value in name starts with search string 'ap'.

let name = 'apple';
let output = name.startsWith('ap');
console.log(output);

2. check if string value in name starts with search string 'ba'.

let name = 'apple';
let output = name.startsWith('ba');
console.log(output);

3. check if string value in name starts with search string 'pp' with a start index of 1.

let name = 'apple';
let output = name.startsWith('pp', 1);
console.log(output);


Copyright @2022