JavaScript String startsWith() Method Last Updated : 11 Jul, 2024 Comments Improve Suggest changes Like Article Like Report JavaScript String startsWith() method checks if a string starts with the characters of a specified string.Syntaxstr.startsWith( searchString , position )ParametersThis method accepts two parameters as mentioned above and described below: searchString: It is a required parameter. It stores the string that needs to be searched.start: It determines the position in the given string from where the searchString is to be searched. The default value is zero.Return ValueThis method returns the Boolean value true if the searchString is found else returns false.String startsWith() ExamplesExample 1: Checking if a String Starts with a Specific SubstringThe function checks if the string 'str' starts with the substring 'Gee'. It returns true because 'Geeks for Geeks' starts with 'Gee'. JavaScript let str = 'Geeks for Geeks'; let value = str.startsWith('Gee'); console.log(value); Outputtrue Example 2: Checking if a String Starts with a Specific Substring at a Given Position The function checks if the string 'str' starts with the substring 'great' starting from index 8. It returns true because 'great day.' starts at index 8 in the string. JavaScript // JavaScript to illustrate // startsWith() method // Original string let str = 'It is a great day.'; let value = str.startsWith('great', 8); console.log(value); Outputtrue Example 3: Checking if a URL or Filename Starts with a Specific SubstringThe code checks if the URL starts with "https://" and if the filename starts with ".js". It returns true for the URL starting with "https://" and false for the filename starting with ".js". JavaScript // Checking if a URL starts with "https://" let url ='https://www.geeksforgeeks.org/'; console.log(url.startsWith('https://')); console.log(url.startsWith('http://')); //Checking if a filename starts with a specific extension let filename = 'script.js'; console.log(filename.startsWith('.js')); console.log(filename.startsWith('script.')); Outputtrue false false true Supported Browser:Google Chrome 41Edge 12Firefox 17Opera 28Safari 9 Comment H HGaur Follow Improve H HGaur Follow Improve Article Tags : Misc JavaScript Web Technologies javascript-string JavaScript-Methods +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like