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

2

62

Is it possible to test a variable to see if it is a primitive?

I have seen lots of questions about testing an variable to see if it is an object, but not testing for a primitive.

This question is academic, I don't actually need to perform this test from my own code. I'm just trying to get a deeper understanding of JavaScript.

Glitter answered 21/7, 2015 at 11:38 Comment(5)
A specific type of primitive, or just any old not-an-object primitive?Benilda
Have you tried using typeof myVar !== 'object' && typeof myVar !== 'string'... ?Microwave
Relevant documentation: typeof operatorPadang
@AnthonyGrist - any old not-an-object primitive.Glitter
This is a duplicate of many questions.Maxia
B
82

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/

Bastia answered 21/7, 2015 at 11:42 Comment(10)
Am I right when I feel that it has a performance overhead as the price of code simplicity?Chatman
@Zoltán Tamási, yeah you are right, about 7 times slower than instanceof checkSuture
Not worth an edit, but you don't need the ; after a function declaration (as opposed to a function expression like var isPrimitive = function(test) {...};).Pumpkinseed
Object(undefined) !== undefined is trueLacto
@Artemiy StagnantIce Alexeew Didn't you expect so? Undefined is a primitive type.Baryram
@ZoltánTamási An alternative would be const isPrimitive = (value) => { try { "" in value; return false; } catch { return true; } };. … in value never throws any errors except in the case where value is a primitive. Without the catch binding, an engine may optimize away any TypeError object.Darlenedarline
@SebastianSimon A general rule that I've heard is to try and avoid using errors as a method of control flow within your codebase, not only are errors highly costly in terms of performance but they reflect something deeper needing resolution, errors are for exceptions and not ordinary behaviour - even if they just return out and resume as normal without actually interrupting the program.Pattypatulous
@ChiaraAni can you elaborate? isPrimitive([]) returns false for me, which is correct.Canea
Sorry, I checked it wrong.Susy
I've run this method against one using typeof (example here) in a benchmark (inside a for loop of 10.000.00.000 iterations). The Object() implementation is only by 0.09% slower than using `typeof``Freed
H
21

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
Housman answered 21/7, 2015 at 11:42 Comment(1)
Am I right when I feel that it has a performance overhead as the price of code simplicity?Chatman

© 2022 - 2024 — McMap. All rights reserved.