Menu

gitpiper

is javascript Code Snippet in 2024

typearrayintermediate

Last Updated: 20 April 2024

Checks if the provided value is of the specified type.

  • Ensure the value is not undefined or null using Array.prototype.includes().
  • Compare the constructor property on the value with type to check if the provided value is of the specified type.
const is = (type, val) => ![, null].includes(val) && val.constructor === type;
is(Array, [1]); // true is(ArrayBuffer, new ArrayBuffer()); // true is(Map, new Map()); // true is(RegExp, /./g); // true is(Set, new Set()); // true is(WeakMap, new WeakMap()); // true is(WeakSet, new WeakSet()); // true is(String, ''); // true is(String, new String('')); // true is(Number, 1); // true is(Number, new Number(1)); // true is(Boolean, true); // true is(Boolean, new Boolean(true)); // true

javascript snippet similar to is For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️