Mastering check data types in Javascript

Mastering check data types in Javascript

How do you check if a variable is a string, number, array, object, function, null & undefined, boolean, regexp, error, date, symbol in JavaScript?

This article will help you check whether a variable or parameter is a string, number, array, object, function, null & undefined, boolean, regexp, error, date, symbol in JavaScript

How do you check if a variable is a String in Javascript

A string is always a string so this one is easy. Except if called with new (new String) typeof will instead return "object". So to also include those strings instanceof can be used.

function isString (value) {
    return typeof value === 'string' || value instanceof String;
}

How do you check if a variable is a Number in Javascript

From typeof more things than just an ordinary number will return "number" like NaN and Infinity. To know if a value really is a number the function isFinite is also required.

function isNumber (value) {
    return typeof value === 'number' && isFinite(value);
}

How do you check if a variable is a Array in Javascript

In javascript arrays are not true arrays

In javascript arrays are not true arrays like in java and in other languages. They're actually objects so typeof will return "object" for them. To know if something's really an array its constructor can be compared to Array.

function isArray (value) {
return value && typeof value === 'object' && value.constructor === Array;
}

// ES5 actually has a method for this (ie9+)
Array.isArray(value);

How do you check if a variable is a Function in Javascript

Functions are functions so here just typeof is enough.

function isFunction (value) {
    return typeof value === 'function';
}

How do you check if a variable is a Object in Javascript

Many things are objects in javascript. To know if a value is an object that can have properties and be looped through, its constructor can be compared to Object. It doesn't work for objects created from classes, then the instanceof operator can be used instead.

function isObject (value) {
return value && typeof value === 'object' && value.constructor === Object;
}

How do you check if a variable is a Null & undefined in Javascript

Most times you don't need to check explicitly for null and undefined since they're both falsy values. However to do it below functions does the trick.

// Returns if a value is null
function isNull (value) {
    return value === null;
}

// Returns if a value is undefined
function isUndefined (value) {
    return typeof value === 'undefined';
}

How do you check if a variable is a Boolean in Javascript

For booleans typeof is enough since it returns "boolean" for both true and false.

// Returns if a value is a boolean
function isBoolean (value) {
    return typeof value === 'boolean';
}

How do you check if a variable is a RegExp in Javascript

RegExp's are objects so the only thing needed to check is if the constructor is RegExp.

// Returns if a value is a regexp
function isRegExp (value) {
    return value && typeof value === 'object' && value.constructor === RegExp;
}

How do you check if a variable is a Error in Javascript

Errors in javascript are the same as "exceptions" in many other programming languages. They come in a couple different forms like for instance Error, TypeError and RangeError. An instanceof statement is enough for them all, but just to be extra sure we also check for the "message" property that errors have.

// Returns if value is an error object
function isError (value) {
return value instanceof Error && typeof value.message !== 'undefined';
}

How do you check if a variable is a Date in Javascript

Date isn't really a data type in Javascript. But to know if something's a Date object it can be checked with instanceof.

// Returns if value is a date object
function isDate (value) {
    return value instanceof Date;
}

How do you check if a variable is a Symbol in Javascript

In ES6 the new datatype Symbol was added. Nicely enough typeof returns "symbol" for it so no more logic is required.

// Returns if a Symbol
function isSymbol (value) {
return typeof value === 'symbol';
}

If useful to you and your friends. Please share.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

JavaScript ES6 Classes

An exciting new construct that was introduced in the ES6 specification is the ES6 classes. If you're a Javascript developer, you will be aware that Javascript follows prototypal inheritance and sometimes it can get a little messy. However, with ES6 classes the syntax is simpler and much more intuitive.

Linked Lists in JavaScript With ES6

Linked Lists in JavaScript with ES6.This series is about data structure implementation in JavaScript using the ES6 specification.. Let's see how to make a singly linked list in Javascript… ... list in Javascript. We'll be using ES6 syntax throughout.

An Introduction to JavaScript ES6 Proxies

Proxy is one of the most overlooked concepts introduced in ES6 version of JavaScript, but ES6 proxies bound to come in handy at some point in your future.