Back to JavaScript
2026-02-125 min read

JavaScript Program to Check if a String Starts With Another String

Learn JavaScript Program to Check if a String Starts With Another String step by step with clear examples and exercises.

Why This Matters

In this comprehensive lesson, we will delve into a JavaScript program that checks whether a string starts with another string. This skill is indispensable for numerous real-world scenarios such as web development, data validation, and text processing. Understanding and mastering the startsWith() method can significantly streamline your workflow when dealing with strings in JavaScript.

The Importance of String Matching in JavaScript

When working with strings in JavaScript, it's crucial to check if one string begins with another. For instance, you might want to validate user input, search for specific patterns, or perform string manipulations based on a given prefix. Mastering the startsWith() method can help you achieve these goals efficiently and effectively.

Prerequisites

Before diving into the core concept, make sure you have a solid understanding of the following topics:

  • Variables and data types in JavaScript
  • Basic string manipulation in JavaScript
  • Control structures like if statements
  • Regular expressions (optional but recommended)

A thorough understanding of these concepts will ensure that you can follow along with this lesson effectively.

Core Concept

To check if a string starts with another string in JavaScript, we can use the built-in startsWith() method. This method returns a boolean value indicating whether the specified string is found at the beginning of the original string or not.

Here's an example:

let str = "Hello World";
let prefix = "He";
if (str.startsWith(prefix)) {
console.log("The string starts with " + prefix);
} else {
console.log("The string does not start with " + prefix);
}

In the above program, we define a string str and a prefix prefix. The startsWith() method is then used to check if the string starts with the specified prefix. If it does, we print a message saying so; otherwise, we print an alternative message.

Understanding startsWith()

The startsWith() method takes one argument: the substring you want to search for at the beginning of the original string. It returns true if the original string starts with the specified substring and false otherwise.

let str = "Hello World";
console.log(str.startsWith("He")); // true
console.log(str.startsWith("Hel")); // false

Case Sensitivity

Note that that the startsWith() method is case-sensitive, meaning it will only match exact cases:

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

To make the comparison case-insensitive, you can use regular expressions as we'll discuss in the next section.

Worked Example

Let's create a simple program that checks if a user-entered string starts with a given prefix:

let prefix = prompt("Enter the prefix to check:");
let userInput = prompt("Enter your string:");
if (userInput.startsWith(prefix)) {
console.log(`Your string "${userInput}" starts with the prefix "${prefix}".`);
} else {
console.log(`Your string "${userInput}" does not start with the prefix "${prefix}".`);
}

In this example, we first prompt the user to enter a prefix and their string. We then use the startsWith() method to check if the user's string starts with the entered prefix. Depending on the result, we print an appropriate message.

Common Mistakes

1. Forgetting to convert input to a string

If you're working with numbers or other data types, remember to convert them to strings before using the startsWith() method:

let prefix = "He";
let userInput = 1234; // number
userInput = String(userInput); // convert to string
// Now you can use startsWith()
if (userInput.startsWith(prefix)) {
console.log("The string starts with " + prefix);
} else {
console.log("The string does not start with " + prefix);
}

2. Case sensitivity

JavaScript is case-sensitive, so be aware that the startsWith() method will only match exact cases:

let str = "Hello World";
if (str.startsWith("hello")) { // false
console.log("The string starts with 'hello'.");
} else {
console.log("The string does not start with 'hello'.");
}

To make the comparison case-insensitive, you can use regular expressions as we will discuss in the next section.

Making Comparisons Case-Insensitive

You can make the comparison case-insensitive by using regular expressions:

let str = "Hello World";
if (str.startsWith(/^hello/i)) { // true
console.log("The string starts with 'hello'.");
} else {
console.log("The string does not start with 'hello'.");
}

In the above example, we use a regular expression that matches any string starting with "hello" regardless of case. The /i flag makes the comparison case-insensitive.

Practice Questions

  1. Write a program that checks if the user's input is an email address starting with "example".
  2. Modify the example program to make the comparison case-insensitive.
  3. Write a function that returns true if a string starts with any of several possible prefixes (e.g., ["He", "Hel", "Hello"]).
  4. Write a function that checks if a string contains a substring, regardless of case.
  5. Write a function that replaces all instances of a substring in a given string, regardless of case.
  6. Write a program that validates a password by checking if it starts with an uppercase letter and ends with a number.
  7. Write a program that checks if a URL begins with "https://" or "http://".
  8. Write a program that checks if a string is a palindrome (reads the same forwards and backwards).
  9. Write a function that extracts all email addresses from a given string.
  10. Write a function that removes all duplicate words from a given string.

FAQ

Q: Does JavaScript have a method to check if a string ends with another string?

A: Yes, the endsWith() method can be used to check if a string ends with another string in JavaScript.

Q: How do I make the comparison case-insensitive when using startsWith() or endsWith()?

A: To make the comparison case-insensitive, you can use regular expressions by prefixing the pattern with /^ and appending /$. For example: str.startsWith(/^prefix$/i).

Q: What if I want to check for multiple possible prefixes?

A: You can create an array of possible prefixes and loop through them, checking each one using the startsWith() method. If any prefix matches, return true; otherwise, false.

Q: How do I use regular expressions with startsWith() or endsWith()?

A: To use regular expressions with startsWith() or endsWith(), simply pass a regular expression as the argument instead of a string. For example: str.startsWith(/^prefix$/).

Q: Can I use startsWith() or endsWith() for substrings in the middle of a string?

A: No, the startsWith() and endsWith() methods only check for substrings at the beginning or end of a string. If you need to find substrings anywhere within a string, consider using regular expressions or other methods like indexOf().

JavaScript Program to Check if a String Starts With Another String | JavaScript | XQA Learn