JavaScript startsWith() method


The startsWith() method in JavaScript is used to determine whether a string begins with a specified substring. This method is particularly useful for checking if a string starts with a certain word or sequence of characters.

Syntax:

string.startsWith(searchString, position)
  • searchString: The substring to search for at the start of the string. This is a required parameter.
  • position (optional): The position in the string at which to begin searching. The default value is 0, meaning the search starts at the beginning of the string.

Return Value:

  • Returns true if the string starts with the specified substring; otherwise, it returns false.

Example 1: Basic Usage

let str = "Hello, World!"; let result = str.startsWith("Hello"); console.log(result); // true

In this example, the method checks if the string starts with "Hello" and returns true.

Example 2: Checking a Different Position

You can specify a different position from which to start the search.

let str = "Hello, World!"; let result = str.startsWith("World", 7); console.log(result); // true

Here, the method checks if the string starting from index 7 begins with "World", and it returns true.

Example 3: Case Sensitivity

The startsWith() method is case-sensitive.

let str = "Hello, World!"; let result = str.startsWith("hello"); console.log(result); // false

In this example, the search for "hello" (lowercase) returns false because it does not match the uppercase "H".

Example 4: Position Out of Range

If the position specified is greater than the length of the string, startsWith() will always return false.

let str = "Hello, World!"; let result = str.startsWith("Hello", 15); console.log(result); // false

In this case, since the starting position 15 exceeds the length of the string, the method returns false.

Example 5: Using Empty Strings

An empty string is considered to start with any other string, including itself.

let str = "Hello, World!"; let result1 = str.startsWith(""); // Empty search string let result2 = "".startsWith(""); // Empty string comparison console.log(result1); // true console.log(result2); // true

In this example, both checks return true because every string, including an empty string, starts with an empty string.

Example 6: Checking for Non-String Values

If a non-string value is passed as the searchString, it is converted to a string using the String() function.

let str = "Hello, World!"; let result = str.startsWith(Hello); console.log(result); // true

In this case, the number Hello is implicitly converted to the string "Hello" before the comparison is made.

Summary:

  • The startsWith() method checks if a string begins with a specified substring.
  • It takes two parameters: searchString (required) and position (optional).
  • The method is case-sensitive and returns true or false based on whether the substring is found at the beginning of the string.
  • An empty string will always return true when checked with startsWith(), and non-string values are converted to strings for comparison.