• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  Using JavaScript to Return String
  • 2.  Reverse For Loop JavaScript
  • 3.  How to Check if a String Contains Vowels in JavaScript
  • 4.  JavaScript sinh – Find Hyperbolic Sine of Number Using Math.sinh()
  • 5.  Using JavaScript to Convert Month Number to Name
  • 6.  JavaScript Sorted Map – How to Sort a Map by Keys or Values
  • 7.  How to Convert Radians to Degrees Using JavaScript
  • 8.  Using JavaScript to Disable a Button
  • 9.  How to Hide a Div in JavaScript
  • 10.  Using JavaScript to Get Image Dimensions

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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