Nội dung bài viết
Video học lập trình mỗi ngày
This article will help you to check data types in Javascript whether a variable or parameter is a string, number, array, object, function, null & undefined, boolean, regexp, error or date.
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) type of will instead return “object”. So to also include those strings instance of can be used.
function isString (value) { return typeof value === 'string' || value instanceof String; }
Notes: Đọc thêm về "String to Array JavaScript"
How do you check if a variable is a Number in Javascript
From type-of 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'; }
Notes: Đọc thêm về "Tips and Tricks Javascript - Làm thế nào để add event listener multiple elements trong javascript"
How do you check if a variable is an 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 instance of the 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
Regexps 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 an Error in Javascript
Errors in javascript are the same as “exceptions” in many other programming languages. They come in a couple of 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'; }
Notes: Đọc thêm về "Tổng hợp những bài viết Mẹo lập trình javascript"
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'; }
Resource: christosploutarchou.com