startsWith()
.طريقة يمكنك تحقق بها إن كان نص
يبدء بالعبارة ما و تعيد لك صحيح
أو خطأ
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
تركيب الجملة | Syntax
str.startsWith(searchString[, position])
المعاملات | Parameters
searchString
- العبارة المبحوث عنها في
النص.
position
Optional- مكان الذي يبدأ البحث منه في
النص
الإفتراضي 0
القيمة العائدة | Return value
العائد يكون صحيح إذا وجد تطابق
و إن لم يجيد تطابق يعيد لك خطأ
الوصف | Description
هذه الطريقة حساسة إتجاه الحروف
case-sensitive
أمثلة | Examples
Using 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
Polyfill
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.
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.prototype.startsWith' in that specification. |
Standard | Initial definition. |
ECMAScript (ECMA-262) The definition of 'String.prototype.startsWith' in that specification. |
Living Standard |
توافق مع متصفحات
BCD tables only load in the browser
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.