--- title: String.prototype.startsWith() slug: Web/JavaScript/Reference/Global_Objects/String/startsWith tags: - Begin - JavaScript - Méthode - String translation_of: Web/JavaScript/Reference/Global_Objects/String/startsWith ---
De startsWith() methode bepaalt of een string begint met de karakters van een bepaalde string. Deze geeft true of false terug waar nodig.
str.startsWith(zoekString[, positie])
zoekStringpositie{{optional_inline}}zoekString; start standaard op 0.true als de karakters teruggevonden worden aan het begin van de string, anders false.
Deze methde laat je nagaan of een string begint met een andere string. Dit is hoofdletter gevoelig
startsWith()//startswith
var str = 'Te nemen of te laten.';
console.log(str.startsWith('Te nemen')); // true
console.log(str.startsWith('te laten')); // false
console.log(str.startsWith('te laten', 12)); // true
Deze methode is toegevoegd aan de ECMAScript 2015 specificaties en is misschien nog niet beschikbaar in alle JavaScript implementaties. Je kan wel Polyfill String.prototype.startsWith() alsvolgt gebruiken
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(search, pos) {
return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
};
}
Een meer degelijke en geoptimaliseerde Polyfill is beschikbaar op GitHub door Mathias Bynens.
| Specificatie | Status | Commentaar |
|---|---|---|
| {{SpecName('ES2015', '#sec-string.prototype.startswith', 'String.prototype.startsWith')}} | {{Spec2('ES2015')}} | Eerste definitie. |
| {{SpecName('ESDraft', '#sec-string.prototype.startswith', 'String.prototype.startsWith')}} | {{Spec2('ESDraft')}} |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
{{Compat("javascript.builtins.String.startsWith")}}