includes()
تستعمل للبحث أو تحقق من عبارة ما إن كانت موجودة في النص أو لا , و تعيد لك صحيح أو خطأ
.
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.includes(searchString[, position])
المعاملات | Parameters
searchString- العبارة الذي تبحث عنها في
النص positionOptional- مكان الذي يبدء البحث منه في النص (الإفتراضي : 0)
القيمة عائدة | Return value
صحيح "true" إذا وجد عبارة مطلوبة و إن لم يجد عائد سيكون خظأ "false"
الوصف | Description
هذه الطريقة تجعلك قادر على التحقق من الأشياء أو العبارات في نصوص
الحروف حساسة | Case-sensitivity
إذ كنت تتعامل مع includes() فالعلم أنها حساس إتجاه الحروف
أنظر للمثال .
'BLUE Whale'.includes('blue'); // returns false
أمثلة | Examples
const str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be')); // true
console.log(str.includes('question')); // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1)); // false
console.log(str.includes('TO BE')); // false
console.log(str.includes('')) // 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 easily polyfill this method:
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
'use strict';
if (typeof start !== 'number') {
start = 0;
}
if (start + search.length > this.length) {
return false;
} else {
return this.indexOf(search, start) !== -1;
}
};
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| ECMAScript Latest Draft (ECMA-262) The definition of 'String.prototype.includes' in that specification. |
Draft | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'String.prototype.includes' in that specification. |
Standard | Initial definition. |
Browser compatibility
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.
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
includes | Chrome Full support 41 | Edge Full support 12 | Firefox
Full support
40
| IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android
Full support
40
| Opera Android Full support Yes | Safari iOS Full support 9 | Samsung Internet Android Full support Yes | nodejs Full support 4.0.0 |
Legend
- Full support
- Full support
- No support
- No support
- Uses a non-standard name.
- Uses a non-standard name.
String.prototype.contains
In Firefox 18 - 39, the name of this method was contains(). It was renamed to includes() in bug 1102219 due to the following reason:
It's been reported that some websites using MooTools 1.2 broke on Firefox 17. This version of MooTools checks whether String.prototype.contains() exists and, if it doesn't, MooTools adds its own function. With the introduction of this function in Firefox 17, the behavior of that check changed in a way that causes code based on MooTools' String.prototype.contains() implementation to break. As a result, the implementation was disabled in Firefox 17 and String.prototype.contains() was available one version later, in Firefox 18, when outreach to MooTools was leading to the release of MooTools version 1.2.6.
MooTools 1.3 forces its own version of String.prototype.contains(), so websites relying on it should not break. However, you should note that MooTools 1.3 signature and ECMAScript 2015 signatures for this method differ (on the second argument). Later, MooTools 1.5+ changed the signature to match the ES2015 standard.
In Firefox 48, String.prototype.contains() has been removed. Use String.prototype.includes() only.