Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

String.prototype.startsWith — checking a string prefix

The startsWith method checks whether a string has a given prefix at its beginning or exactly at a specified position. It returns a boolean, is case-sensitive, and does not modify the string.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

string.startsWith(searchString[, position])

String.prototype.startsWith returns true when searchString matches exactly at index position; otherwise, it returns false. It does not search later positions and does not modify the string.

The default position is 0. The value is converted to an integer by truncating its fractional part toward zero, then clamped between 0 and the string length; NaN means 0. Indices count UTF-16 code units, not necessarily complete user-perceived characters.

Comparison is case-sensitive and does not normalize Unicode. An empty searchString returns true, even when the supplied position exceeds the string length. Other values are converted to strings: omitting the argument means searching for "undefined". An ordinary regular expression causes a TypeError.

The method can operate on other receivers through call, converting them to strings, but a null or undefined receiver causes a TypeError. This is an ECMAScript language API, not a DOM API; the specification alone does not establish support in a particular browser.

Examples

const text = "Hello, world!";
console.log(text.startsWith("Hello"));
console.log(text.startsWith("hello"));
console.log(text.startsWith("world", 7));
console.log(text.startsWith("world", 6));
console.log(text.startsWith("", 99));

Result

true
false
true
false
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources