JavaScript Program to Check Whether a String Starts and Ends With Certain Characters

Updated on November 11, 2024
Check whether a string starts and ends with certain characters header image

Introduction

In JavaScript, checking whether a string starts or ends with specific characters is a fundamental task that you might often need to perform in various applications. This capability is especially useful in form validations, parsing input data, and implementing search functionalities where precise string matching is required.

In this article, you will learn how to check if a string starts and ends with certain characters using straightforward examples. Explore how to employ built-in string methods to make these determinations efficiently.

Checking the Start of a String

Using the startsWith() Method

  1. Understand that the startsWith() method determines if a string begins with the characters of a specified string.

  2. Prepare a string variable and apply the startsWith() method.

    javascript
    let str = "Hello world!";
    let startsWithHello = str.startsWith("Hello");
    console.log(startsWithHello);  // Outputs: true
    

    The startsWith() method checks the variable str to see if it begins with "Hello". Since it does, the output is true.

Case Sensitivity Consideration

  1. Remember that startsWith() is case-sensitive.

  2. Test the function with a different case to see the effect.

    javascript
    let str = "hello world!";
    let startsWithHello = str.startsWith("Hello");
    console.log(startsWithHello);  // Outputs: false
    

    In this example, even though str starts with "hello", because startsWith() is case-sensitive and "Hello" is not exactly the same as "hello", the result is false.

Checking the End of a String

Using the endsWith() Method

  1. Know that the endsWith() method is used to check if a string ends with the characters of a specified string, similar to startsWith() but for the end of the strings.

  2. Create a string variable and apply the endsWith() method.

    javascript
    let str = "Hello world!";
    let endsWithWorld = str.endsWith("world!");
    console.log(endsWithWorld);  // Outputs: true
    

    Here, str is checked to see if it ends with "world!". Since it does, the output is true.

Case Sensitivity in endsWith()

  1. Note the case sensitivity in endsWith() similar to startsWith().

  2. Test a case variation to confirm.

    javascript
    let str = "Hello World!";
    let endsWithWorld = str.endsWith("world!");
    console.log(endsWithWorld);  // Outputs: false
    

    As endsWith() is also case-sensitive, "World!" and "world!" do not match due to the uppercase "W", thus the result is false.

Conclusion

The startsWith() and endsWith() methods in JavaScript offer a robust and straightforward approach to check if strings start or end with certain characters. These methods are invaluable for various programming scenarios, particularly those involving string manipulation and validation. By mastering these techniques, you enhance your ability to handle strings effectively, ensuring your applications can perform precise string matching and validations where necessary.