• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / JavaScript / JavaScript String startsWith Method

JavaScript String startsWith Method

August 29, 2022 Leave a Comment

Using the JavaScript String startsWith() method, we can check to see if a string starts with a certain character or string. The startsWith method is used on a string and takes a string as its parameter.

someString.startsWith("anotherString");

Let’s take a look at an example of this below.


Let’s say we have the following JavaScript:

var someString = "This is some example text";

If we want to see if the string someString starts with the string “This”, then we can use the startsWith() method to let us know.

var someString = "This is some example text";

console.log(someString.startsWith("This"));

#Output
true

The startsWith method is case-sensitive, so note that if we tried entering in “this” in lowercase, we would get a different result.

var someString = "This is some example text";

console.log(someString.startsWith("this"));

#Output
false

Check if a String Ends with a Certain Character or String

To check if a String ends with a different string, we can use the endsWith() method. This works the same way as the startsWith() method but just checks at the end of the string, not the start.

someString.endsWith("anotherString");

Similarly as we did above, if we want to check if the string someString ends with the string “text”, then we can use the endsWith() method to let us know.

var someString = "This is some example text";

console.log(someString.endsWith("text"));

#Output
true

The endsWith method is also case-sensitive.

Hopefully this article has been useful for you to learn how to use the JavaScript String startsWith method.

Other Articles You'll Also Like:

  • 1.  Unveiling the Magic: JavaScript Show and Hide Div
  • 2.  JavaScript Square Root with Math.sqrt() Method
  • 3.  Using JavaScript to Check If String Contains Only Certain Characters
  • 4.  How to Clear an Input Field in JavaScript
  • 5.  Using JavaScript to Replace Character in String
  • 6.  JavaScript Change Text Color of a Paragraph
  • 7.  Using JavaScript to Run a Function Every 5 seconds
  • 8.  Using JavaScript to Get the Length of Array
  • 9.  Reverse a String in JavaScript
  • 10.  Remove Word From String In JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Copyright © 2023 · The Programming Expert · About · Privacy Policy