Write a JavaScript function to check the types for each
- isArray – returns true for an array, else false
- isFunction – returns true for functions, else false
- isObject – returns true for objects, else false
- isPlainObject – returns true for plain objects; otherwise, it is false (arrays, functions, dates, etc.).
🔗 Type Utilities 🟢 Easy🧩 Pattern – JavaScript Essentials
Note ✍️
const obj = {}; // or: const obj = new Object();Code language: JavaScript (javascript)This object inherits from Object.prototype, so it has access to built-in methods like .toString(), .hasOwnProperty().
const obj = Object.create(null);Code language: JavaScript (javascript)This means it doesn’t inherit any properties or methods (like .toString, .hasOwnProperty, etc.) but it is an object.
JS Code to check all types
export function isArray(value) {
return Array.isArray(value);
}
export function isFunction(value) {
return typeof value === "function";
}
export function isObject(value) {
if (value === null) {
return false;
}
return typeof value === "object" || typeof value === "function";
}
export function isPlainObject(value) {
if (typeof value !== "object" || value === null) return false;
const proto = Object.getPrototypeOf(value);
return proto === Object.prototype || proto === null;
}Code language: JavaScript (javascript)