[javascript] How do I check for null values in JavaScript?

I found a another way to test if the value is null:

if(variable >= 0 && typeof variable === "object")

null acts as a number and object at the same time. Comparing null >= 0 or null <= 0 results in true. Comparing null === 0 or null > 0 or null < 0 will result in false. But as null is also an object we can detect it as a null.

I made a more complex function natureof witch will do better than typeof and can be told what types to include or keep grouped

_x000D_
_x000D_
/* function natureof(variable, [included types])_x000D_
included types are _x000D_
    null - null will result in "undefined" or if included, will result in "null"_x000D_
    NaN - NaN will result in "undefined" or if included, will result in "NaN"_x000D_
    -infinity - will separate negative -Inifity from "Infinity"_x000D_
    number - will split number into "int" or "double"_x000D_
    array - will separate "array" from "object"_x000D_
    empty - empty "string" will result in "empty" or_x000D_
    empty=undefined - empty "string" will result in "undefined"_x000D_
*/_x000D_
function natureof(v, ...types){_x000D_
/*null*/            if(v === null) return types.includes('null') ? "null" : "undefined";_x000D_
/*NaN*/             if(typeof v == "number") return (isNaN(v)) ? types.includes('NaN') ? "NaN" : "undefined" : _x000D_
/*-infinity*/       (v+1 === v) ? (types.includes('-infinity') && v === Number.NEGATIVE_INFINITY) ? "-infinity" : "infinity" : _x000D_
/*number*/          (types.includes('number')) ? (Number.isInteger(v)) ? "int" : "double" : "number";_x000D_
/*array*/           if(typeof v == "object") return (types.includes('array') && Array.isArray(v)) ? "array" : "object";_x000D_
/*empty*/           if(typeof v == "string") return (v == "") ? types.includes('empty') ? "empty" : _x000D_
/*empty=undefined*/ types.includes('empty=undefined') ? "undefined" : "string" : "string";_x000D_
                    else return typeof v_x000D_
}_x000D_
_x000D_
// DEMO_x000D_
let types = [null, "", "string", undefined, NaN, Infinity, -Infinity, false, "false", true, "true", 0, 1, -1, 0.1, "test", {var:1}, [1,2], {0: 1, 1: 2, length: 2}]_x000D_
_x000D_
for(i in types){_x000D_
console.log("natureof ", types[i], " = ", natureof(types[i], "null", "NaN", "-infinity", "number", "array", "empty=undefined")) _x000D_
}
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to null

getElementById in React Filter values only if not null using lambda in Java8 Why use Optional.of over Optional.ofNullable? How to resolve TypeError: Cannot convert undefined or null to object Check if returned value is not null and if so assign it, in one line, with one method call How do I assign a null value to a variable in PowerShell? Using COALESCE to handle NULL values in PostgreSQL How to check a Long for null in java Check if AJAX response data is empty/blank/null/undefined/0 Best way to check for "empty or null value"

Examples related to compare

Checking for duplicate strings in JavaScript array How to compare two files in Notepad++ v6.6.8 How to compare LocalDate instances Java 8 Comparing the contents of two files in Sublime Text comparing elements of the same array in java How to compare two dates along with time in java bash string compare to multiple correct values Query comparing dates in SQL How to compare two java objects Comparing two integer arrays in Java

Examples related to comparison

Wildcard string comparison in Javascript How to compare two JSON objects with the same elements in a different order equal? Comparing strings, c++ Char Comparison in C bash string compare to multiple correct values Comparing two hashmaps for equal values and same key sets? Comparing boxed Long values 127 and 128 Compare two files report difference in python How do I fix this "TypeError: 'str' object is not callable" error? Compare cell contents against string in Excel