JavaScript String startsWith()

Total
4
Shares

In JavaScript, the startsWith() method determines whether the given string begins with a specific sequence of characters. Let’s take a look at syntax and examples of the Javascript string startsWith() method.

Note: If you want to determine whether the given string ends with a specific sequence of characters then you can use JavaScript String endsWith()

startsWith() Syntax

The syntax of the startsWith() method is:

startsWith(searchString, position)

startsWith() Parameters

The startsWith() method takes two parameters.

  • searchString (required): The characters that need to be searched at the start of the string.
  • position (optional): The position of the string at which it needs to begin the search. It’s an optional parameter, and if the value is not specified, it defaults to position 0. 

startsWith() Return Value

The startsWith() function returns true if the given characters are found at the beginning of the string. Otherwise, it returns false.

Note: The startsWith() method is case-sensitive.

JavaScript String startsWith() examples

In this example, you can notice that the startsWith() method is case-sensitive as the exact match returns true else returns false, as shown in the below code.

const text = 'Hello, Welcome to JavaScript World';
console.log(text.startsWith('Hello')); // true
console.log(text.startsWith('hello')); // false

Output

true
false

Since we have not given any position parameter, it will start from position 0 of the string.

JavaScript startsWith() example with Position parameter

If we do not specify the position parameter, the string search will begin from position 0, and if the string doesn’t match the given “searchString,” the method returns false, as shown below.

In the below example, we are searching Welcome, which is at position six, but since we have not specified the position parameter, it searches from the 0th position and does not match the search string and returns false.

const text = 'Hello, Welcome to JavaScript World';
console.log(text.startsWith('Welcome')); // false

Output

false

Now, when we pass the exact position as a parameter where the string begins, the startsWith() method will begin the search from the specified position and match the string as shown in the below code.

const text = 'Hello, Welcome to JavaScript World';
console.log(text.startsWith('Welcome',6)); //true

Output

true
1 comment
Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
JavaScript Array slice()

JavaScript Array slice()

The slice() method in JavaScript returns a shallow copy of a portion of an array as a new array. The original array remains unchanged. slice() Example slice() Syntax The syntax…
View Post
JavaScript String split()

JavaScript String split()

In this tutorial, we will learn about the JavaScript String split() method with examples. The JavaScript String split() method splits a string into an ordered list of strings and returns…
View Post
JavaScript String slice()

JavaScript String slice()

In this tutorial, we will learn about the JavaScript String slice() method with examples. The JavaScript String slice() method extracts a section of the string and returns it as a…
View Post
JavaScript String toUpperCase()

JavaScript String toUpperCase()

In this tutorial, we will learn about the JavaScript String toUpperCase() method with examples. The JavaScript String toUpperCase() method converts the string into uppercase and returns a new string. toUpperCase()…
View Post
JavaScript String endsWith()

JavaScript String endsWith()

In JavaScript, the endsWith() method determines whether the given string ends with a specific sequence of characters. Let’s take a look at syntax and examples of the Javascript string endsWith()…
View Post