JavaScript supports tons of string operations to help you easily work with text. Sometimes you may need to check if a given JavaScript string starts with another string in JavaScript. In this article, we will learn how to do this.
How to Check if String Starts With Another String in JavaScript
Starting ES6 most modern browsers support a function startsWith() that is available to all strings and allows you to check if it starts with another string.
Here are a couple of examples to use this function in JS.
"Hello World!".startsWith("Hello") //returns true
You can also call it on string variables as shown below.
var sample1 = "Hello world"; var sample2 = 'Boy'; console.log(sample1.startsWith(sample2)); // returns false
If you are working with an older browser where startsWith() function is not supported, then you need to use the shim library in your web page, for this purpose.
In this article, we have learnt how to check if a JS string starts with another string.
Also read:
How to Check if Variable is Undefined in JavaScript
How to Detect Invalid Date in JavaScript
Forbidden Characters in Windows & Linux Filenames
MySQL Date Format DD/MM/YYYY in SQL Query
How to Fix MySQL Error 1153
Related posts:
How to Get Length of JavaScript Object
How to Detect Click Outside Element in JavaScript
How to Get Last Item in JS Array
JavaScript Round to 2 Decimal Places
How to Find Sum of Array of Numbers in JavaScript
How to Store Data in Local Storage in JavaScript
How to Break ForEach Loop in JavaScript
How to Return Response from Asynchronous Call in JavaScript

Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.