test if a variable is a primitive rather than an object? [duplicate]

Solution 1:

To test for any primitive:

function isPrimitive(test) {
    return test !== Object(test);
}

Example:

isPrimitive(100); // true
isPrimitive(new Number(100)); // false

http://jsfiddle.net/kieranpotts/dy791s96/

Solution 2:

Object accepts an argument and returns if it is an object, or returns an object otherwise.

Then, you can use a strict equality comparison, which compares types and values.

If value was an object, Object(value) will be the same object, so value === Object(value). If value wasn't an object, value !== Object(value) because they will have different types.

So you can use

Object(value) !== value