Javascript String startsWith()

Introduction

Javascript String startsWith() searches a string for a given substring and return a boolean indicating whether it is included.

str.startsWith(searchString[, position])
  • searchString - the characters to search.
  • position - Optional, the position to begin searching. Defaults to 0.

startsWith() checks for a match beginning at index 0.

let message = "foobarbaz"; 
console.log(message.startsWith("foo"));  // true 
console.log(message.startsWith("bar"));  // false 

The startsWith() method accept an optional second argument that indicates the position to start searching.

This method will start searching from that position and go toward the end of the string, ignoring everything before the start position.

let message = "foobarbaz"; 
            // w  w  w.j  ava 2  s  . c o m
console.log(message.startsWith("foo"));     // true 
console.log(message.startsWith("foo", 1));  // false 



PreviousNext

Related