IDBKeyRange.includes()

The includes() method of the IDBKeyRange interface returns a boolean indicating whether a specified key is inside the key range.

Note: This feature is available in Web Workers.

Syntax

JavaScript
myIncludesResult = myKeyRange.includes('A');

Parameters

key
The key you want to check for in your key range. This can be any type.

Returns

A Boolean.

Exceptions

This method may raise a DOMException of the following type:

Attribute Description
DataError The supplied key was not a valid key.

Example

JavaScript
var keyRangeValue = IDBKeyRange.bound('A', 'K', false, false);

var myResult = keyRangeValue.includes('F');
// Returns true

var myResult = keyRangeValue.includes('W');
// Returns false

Specification

Specification Status Comment
Indexed Database API (Second Edition)
The definition of 'includes()' in that specification.
Recommendation  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
includes()

52.0

47.0 (47.0) ? 39 ?
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Phone Opera Mobile Safari Mobile Chrome for Android
includes() No support 52.0 ? ? ? 39 ? 52.0

Polyfill

The includes() method was added in the second edition of the Indexed DB specification. For browsers that do not support it, the following polyfill can be used.

JavaScript
IDBKeyRange.prototype.includes = IDBKeyRange.prototype.includes || function(key) {
  var r = this, c;
  if (r.lower !== undefined) {
    c = indexedDB.cmp(key, r.lower);
    if (r.lowerOpen && c <= 0) return false;
    if (!r.lowerOpen && c < 0) return false;
  }
  if (r.upper !== undefined) {
    c = indexedDB.cmp(key, r.upper);
    if (r.upperOpen && c >= 0) return false;
    if (!r.upperOpen && c > 0) return false;
  }
  return true;
};

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/idbkeyrange/includes

API Database IDBKeyRange includes IndexedDB Method Reference storage Storage