JavaScript String startsWith() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.startsWith(searchString, position?) returns true if the string begins with the given prefix, otherwise false. Matching is case-sensitive (ES2015). Need a suffix check? Use endsWith(). Need “anywhere”? Prefer includes().

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.startsWith. Start with this line.

JavaScript
str.startsWith(searchString, position?)
CallWhat it does
str.startsWith(searchString, position?)Primary signature.
string.startsWith(...)Call with the arguments your case needs.

Built into JavaScript. Call startsWith(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
str.startsWith(searchString, position?)

Parameters

ParameterDescription
searchStringRequired. Prefix to test (not a RegExp)
positionOptional. Index where the prefix must begin; default 0

Return Value

Return ValueDescription
*true or false

JavaScript Examples

Example 1 — Basic startsWith()

Ask whether a string begins with a given prefix.

JavaScript
const str = "Saturday night plans";

console.log(str.startsWith("Sat"));     // true
console.log(str.startsWith("Sat", 3));  // false
console.log(str.startsWith("night"));   // false
Try It Yourself

How It Works

  1. Default start is 0. Characters 0–2 are "Sat" → true.
  2. Position moves the start. From index 3 the text is "urday…" → false.
  3. Mid-string text is not a prefix. "night" appears later, so startsWith says no.
  4. Use includes for “anywhere”. Prefer includes() when location does not matter.

Example 2 — Prefix from a Position

position sets where the prefix must sit — it does not search forward.

JavaScript
const str = "To be, or not to be, that is the question.";

console.log(str.startsWith("To be"));         // true
console.log(str.startsWith("not to be"));     // false
console.log(str.startsWith("not to be", 10)); // true
Try It Yourself

How It Works

  1. Normal start. The sentence begins with "To be" → true.
  2. Wrong place. Without a position, "not to be" is not at index 0 → false.
  3. Index 10 is the n. Checking from there makes the prefix match → true.
  4. Think “new start”. position redefines the beginning; it is not a search window scan.

Example 3 — Case Sensitivity and Empty Prefix

Exact matches only — and the empty-string quirk.

JavaScript
console.log("File.PDF".startsWith("file"));                 // false
console.log("File.PDF".toLowerCase().startsWith("file"));   // true

console.log("Hello".startsWith(""));  // true
console.log("".startsWith(""));       // true
Try It Yourself

How It Works

  1. Case matters. "File" is not the same as "file" → false.
  2. Normalize first. Lower the string, then check the lower prefix → true.
  3. Empty prefix. startsWith("") is always true, even on "".
  4. Guard user input. Reject empty search strings if that “always true” result is unwanted.

Browser & Runtime Support

String.prototype.startsWith() is part of ES2015 (ES6). It works in all modern browsers and Node.js versions in common use today.

✓ Baseline · ES2015

String.prototype.startsWith()

Supported in Chrome 41+, Firefox 17+, Safari 9+, Edge 12+, and Node.js 4+. Not available in Internet Explorer without a polyfill.

98% Modern browser support
Google Chrome 41+ · Desktop & Mobile
Full support
Mozilla Firefox 17+ · Desktop & Mobile
Full support
Apple Safari 9+ · macOS & iOS
Full support
Microsoft Edge 12+ · Chromium & Legacy
Full support
Internet Explorer Not supported · Polyfill needed
No support
Opera 28+ · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js 4+ · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
startsWith() Excellent

Bottom line: Safe for modern web apps and current Node LTS. For IE11, feature-detect with typeof String.prototype.startsWith === "function" or fall back to str.slice(0, prefix.length) === prefix.

Frequently Asked Questions

startsWith(searchString, position?) returns true if the string begins with searchString (optionally checked from position), otherwise false. The search is case-sensitive.
Yes. "Hello".startsWith("hello") is false. Normalize both sides with toLowerCase() when you need a case-insensitive prefix check.
No. If searchString is a RegExp, startsWith() throws TypeError. Use RegExp.test() with a leading ^ for pattern prefixes.
true for any string. An empty search string is treated as a match at the start.
position is the index where the prefix must begin (default 0). For example "abcdef".startsWith("de", 3) is true because indexes 3–4 are "de".
startsWith() only checks the beginning (or from position as a new start). includes() looks for the substring anywhere in the remaining text.

Next: match()

Learn how to match a string against a regular expression.

match() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful