This content originally appeared on DEV Community and was authored by Mitchell
Introduction
Checking types is a common practice in JavaScript in everyday coding and technical interviews.
You can find all the code in this post at Github.
Primitive values
In JavaScript, all types except Object define immutable values represented directly at the lowest level of the language. We refer to values of these types as primitive values.
There are 7 primitive values:
NullUndefinedBooleanNumberBigIntStringSymbol
All primitive types, except null, can be tested by the typeof operator. typeof null returns "object", so one has to use === null to test for null.
Therefore, we get the first type utility function.
function isBoolean(value) {
return typeof value === 'boolean';
}
function isString(value) {
return typeof value === 'string';
}
function isNumber(value) {
return typeof value === 'number';
}
function isSymbol(value) {
return typeof value === 'symbol';
}
function isBigInt(value) {
return typeof value === 'bigint';
}
function isUndefined(value) {
return typeof value === 'undefined';
}
function isNull(value) {
return value === null;
}
// Usage example
console.log(isSymbol(Symbol('test'))); // => true
console.log(isNull(null)); // => true
console.log(isUndefined(undefined)); // => true
console.log(isNumber(1)); // => true
console.log(isString('')); // => true
console.log(isBoolean(true)); // => true
console.log(isBigInt(9007199254740991n)); // => true
Objects
Everything that's not a primitive type is an object in JavaScript. This includes:
- Plain objects
- Arrays
- Functions
- Dates
- RegExps
- Other built-in object types
Here comes the second utility function for Arrays, Functions, Objects.
function isArray(value) {
return Array.isArray(value);
}
function isFunction(value) {
return typeof value === 'function';
}
function isObject(value) {
// for null and undefined
if (value == null) {
return false;
}
return typeof value === 'object';
}
function isPlainObject(value) {
// for null and undefined
if (value == null) {
return false;
}
const prototype = Object.getPrototypeOf(value);
return prototype === Object.prototype || prototype === null;
}
// Usage example
console.log(isArray(new Array())); // => true
console.log(isObject(Object(null))); // => true
console.log(isFunction(Object.prototype.toString)); // => true
console.log(isPlainObject(Object.create(null))); // => true
Object.prototype.toString.call()
There are several methods to check types in JavaScript, including:
-
typeoffor all the primitive types exceptnull. -
instanceofdetermines whether an object is an instance of a specific constructor or class. It does not work with primitive values.
Object.prototype.toString.call() is the most reliable method for type checking in JavaScript.
We can extract the types by:
function getType(value) {
const type = typeof value;
if (type !== 'object') {
return type;
}
return Object.prototype.toString
.call(value)
.slice(8, -1)
.toLowerCase();
}
// Usage example
console.log(getType(1)); // => number
console.log(getType('')); // => string
console.log(getType({})); // => object
console.log(getType(null)); // => null
console.log(getType(undefined)); // => undefined
console.log(getType(Symbol())); // => symbol
console.log(getType(BigInt(1234567890123456789012345))); // => bigint
console.log(getType(function () {})); // => function
console.log(getType(new Date())); // => date
console.log(getType(new Map())); // => map
console.log(getType(new Set())); // => set
console.log(getType(new RegExp("cat", "i"))); // => regex
Reference
- JavaScript data types and data structures - MDN
- Data types - JavaScript.info
- Reference Type - JavaScript.info
- GreatFrontEnd
This content originally appeared on DEV Community and was authored by Mitchell
Mitchell | Sciencx (2024-10-29T02:39:40+00:00) Type Utilities – JavaScript Challenges. Retrieved from https://www.scien.cx/2024/10/29/type-utilities-javascript-challenges/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.