Methods
getType(value) → {String}
More consistent variety of typeof
. It returns a string such as Null
, Undefined
, Function
, String
, etc.
These values mostly correspond to the value's constructor names.
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
String
Example
// getType() is not limited to the examples below
const v1 = null;
getType(v1); // "Null"
let v2;
getType(v2); // "Undefined"
const v3 = function() {};
getType(v3); // "Function", use isCallable() to cover async functions and generators as well
const v4 = async function() {};
getType(v4); // "AsyncFunction"
const v5 = function* generator(i) {};
getType(v5); // "GeneratorFunction"
const v6 = { foo: "bar" };
getType(v6); // "PlainObject", not "Object", as you may have expected
const v7 = new Map();
getType(v7); // "Map"
const v8 = new Set();
getType(v8); // "Set"
const v9 = new WeakMap();
getType(v9); // "WeakMap"
const v10 = new WeakSet();
getType(v10); // "WeakSet"
const v11 = new Date();
getType(v11); // "Date"
const v12 = new Error();
getType(v12); // "Error"
const v13 = new Promise(() => {});
getType(v13); // "Promise"
class CustomClass {}
const v14 = new CustomClass();
getType(v14); // "CustomClass"
getType(class CustomClass {}) // "Class", note the difference: CustomClass has not been initiated yet
isArray(value) → {Boolean}
Check if a value is an Array
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = [];
isArray(v1); // true
const v2 = new Array(9);
isArray(v2); // true
const v3 = Array.from([1, 2, 3], x => x + x));
isArray(v3); // true
isAsyncFunction(value) → {Boolean}
Check if a value is an AsyncFunction
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = async(() => {});
isAsyncFunction(v1); // true
isFunction(v1); // false, because this covers regular functions only
isBigInt(value) → {Boolean}
Check if a value is a BigInt
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const value = BigInt(123);
isBigInt(value); // true
isNumber(value); // false
isBlob(value) → {Boolean}
Check if a value is a Blob
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const obj = { foo: 'bar' };
const v1 = new Blob([JSON.stringify(obj, null, 2)], { type : 'application/json' });
isBlob(v1); // true
isBoolean(value) → {Boolean}
Check if a value is a Boolean
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = true;
isBoolean(v1); // true
isCallable(value) → {Boolean}
Check if a value is callable
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
class Foo {
anyMethod(){...}
static otherMethod(){...}
}
const fooObj = new Foo();
const v1 = fooObj.anyMethod;
isCallable(v1); // true
const v2 = Foo.otherMethod;
isCallable(v2); // true
const v3 = function () {}
isCallable(v3); // true
const v4 = function* generator(i) {};
isCallable(v4); // true
const v5 = async function () {};
isCallable(v5); // true
isCustomElement(value) → {Boolean}
Check if a value is a custom element
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
class MyElement extends HTMLElement {}
customElements.define("my-element", MyElement);
const v1 = document.createElement("my-element");
isCustomElement(v1); // true
isDate(value) → {Boolean}
Check if a value is a Date object
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = new Date();
isDate(v1); // true
isElement(value) → {Boolean}
Check if a value is a Element
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = document.createElement("div");
isElement(v1); // true
const v2 = document.createElementNS("http://www.w3.org/2000/svg", "svg");
isElement(v2); // true
const v3 = document.createElementNS("http://www.w3.org/1998/Math/MathML", "mrow");
isElement(v3); // true
class MyElement extends HTMLElement {}
customElements.define("my-element", MyElement);
const v4 = document.createElement("my-element");
isElement(v4); // true
isElementCollection(value) → {Boolean}
Check if a value is either a NodeList or an HTMLCollection
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = document.forms;
isElementCollection(v1); // true
const v2 = document.querySelectorAll("li");
isElementCollection(v2); // true
const v3 = Array.from(document.forms);
isElementCollection(v3); // false
const v4 = Array.from(document.querySelectorAll("li"));
isElementCollection(v4); // false
isEmptyObject(value) → {Boolean}
Check if a value is an empty plain object
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = {a:1, b:2};
isEmptyObject(v1); // false
const v2 = {};
isEmptyObject(v2); // true
isError(value) → {Boolean}
Check if a value is an Error
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = new Error();
isError(v1); // true
isFunction(value) → {Boolean}
Check if a value is a Function
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = function() {};
isFunction(v1); // true
const v2 = new Function();
isFunction(v2); // true
const v3 = () => {};
isFunction(v3); // true
const v4 = function* generator(i) {};
isFunction(v4); // false
isGeneratorFunction(v4); // true
isGeneratorFunction(value) → {Boolean}
Check if a value is a GeneratorFunction
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const value = function* generator(i) {};
isGeneratorFunction(value); // true
isFunction(value); // false
isHtmlElement(value) → {Boolean}
Check if a value is an HTML element
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = document.createElement("div");
isHtmlElement(v1); // true
class MyElement extends HTMLElement {}
customElement("my-element", MyElement);
const v2 = document.createElement("my-element");
isHtmlElement(v2); // true
isIterable(value) → {Boolean}
Check if a value is an Iterable
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = [1, 2, 3];
isIterable(v1); // true
const v2 = new Map();
isIterable(v2); // true
const v3 = {};
isIterable(v3); // false
isMap(value) → {Boolean}
Check if a value is a Map
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = new Map();
isMap(v1); // true
isMathElement(value) → {Boolean}
Check if a value is a Math element
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = document.createElementNS("http://www.w3.org/1998/Math/MathML", "mrow");
isMathElement(v1); // true
isNull(value) → {Boolean}
Check if a value is null
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = null;
isNull(v1); // true
isNumber(value) → {Boolean}
Check if a value is a Number
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = 1;
isNumber(v1); // true
const v2 = new Number(1);
isNumber(v2); // true
const v3 = 1.1;
isNumber(v3); // true
const v4 = 42n;
isNumber(v4); // false
isBigInt(v4); // true
isPlainObject(value) → {Boolean}
Check if a value is a plain object
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const value = {a:1, b:2};
isPlainObject(value); // true
getType(value) // "PlainObject"
isPromise(value) → {Boolean}
Check if a value is a Promise
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = new Promise((resolve) => {});
isPromise(v1); // true
isRegExp(value) → {Boolean}
Check if a value is a RegExp
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = /regexp/;
isRegExp(v2); // true
const v3 = new RegExp("regexp", 'gi');
isRegExp(v3); // true
isSet(value) → {Boolean}
Check if a value is a Set
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = new Set();
isSet(v1); // true
isString(value) → {Boolean}
Check if a value is a String
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = "foo";
isString(v1); // true
const v2 = new String("foo");
isString(v2); // true
const v3 = String('foo');
isString(v3); // true
isSvgElement(value) → {Boolean}
Check if a value is an SVG element
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = document.createElementNS("http://www.w3.org/2000/svg", "svg");
isSvgElement(v1); // true
isSymbol(value) → {Boolean}
Check if a value is a Symbol
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = Symbol('foo');
isSymbol(v1); // true
isUndefined(value) → {Boolean}
Check if a value is undefined
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
let foo;
isUndefined(foo); // true
isWeakMap(value) → {Boolean}
Check if a value is a WeakMap
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = new WeakMap();
isWeakMap(v1); // true
isWeakSet(value) → {Boolean}
Check if a value is a WeakSet
- Source:
Parameters:
Name | Type | Description |
---|---|---|
value |
*
|
The value to check |
Returns:
- Type:
-
Boolean
Example
const v1 = new WeakSet();
isWeakSet(v1); // true