--- title: String.prototype.startsWith() slug: Web/JavaScript/Reference/Global_Objects/String/startsWith translation_of: Web/JavaScript/Reference/Global_Objects/String/startsWith ---
startsWith()
.طريقة يمكنك تحقق بها إن كان نص
يبدء بالعبارة ما و تعيد لك صحيح
أو خطأ
str.startsWith(searchString[, position])
searchString
النص.
position
{{optional_inline}}النص
الإفتراضي 0العائد يكون صحيح إذا وجد تطابق
و إن لم يجيد تطابق يعيد لك خطأ
هذه الطريقة حساسة إتجاه الحروف
case-sensitive
startsWith()
//startswith var 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
This method has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet. However, you can polyfill String.prototype.startsWith()
with the following snippet:
if (!String.prototype.startsWith) { Object.defineProperty(String.prototype, 'startsWith', { value: function(search, pos) { pos = !pos || pos < 0 ? 0 : +pos; return this.substring(pos, pos + search.length) === search; } }); }
A more robust (fully ES2015 specification compliant), but less performant and compact, Polyfill is available on GitHub by Mathias Bynens.
Specification | Status | Comment |
---|---|---|
{{SpecName('ES2015', '#sec-string.prototype.startswith', 'String.prototype.startsWith')}} | {{Spec2('ES2015')}} | Initial definition. |
{{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")}}