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
Leave a Reply

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

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like
JavaScript String padEnd()

JavaScript String padEnd()

Table of Contents Hide padEnd() ExamplepadEnd() SyntaxpadEnd() ParameterpadEnd() Return ValueExample 1 – How padEnd() method works in JavaScript?Example 2 – Working of padEnd() method with default value In this tutorial,…
View Post
JavaScript Anonymous Functions

JavaScript Anonymous Functions

Table of Contents Hide Anonymous Functions Syntax Example 1: Anonymous Functions in JavaScriptExample 2: Anonymous Functions with arguments in JavaScriptExample 3: Using anonymous functions as arguments of other functionsExample 4: Immediate…
View Post