58

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Chris Snow
  • 23,813
  • 35
  • 144
  • 309

2 Answers2

78

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/

zeckdude
  • 15,877
  • 43
  • 139
  • 187
kieranpotts
  • 1,510
  • 11
  • 8
  • 2
    Am I right when I feel that it has a performance overhead as the price of code simplicity? – Zoltán Tamási Sep 28 '18 at 14:54
  • 1
    @Zoltán Tamási, yeah you are right, about 7 times slower than instanceof check – ZhenyaUsenko Oct 31 '18 at 12:17
  • 1
    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) {...};`). – Justin Morgan - On strike Feb 06 '19 at 17:34
  • Object(undefined) !== undefined is true – Artemiy StagnantIce Alexeew May 27 '19 at 12:20
  • 2
    @Artemiy StagnantIce Alexeew Didn't you expect so? Undefined is a primitive type. – Hugo Passos Oct 21 '19 at 17:54
  • @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. – Sebastian Simon May 14 '20 at 17:54
  • @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. – Vix Feb 03 '21 at 15:22
  • @ChiaraAni can you elaborate? `isPrimitive([])` returns `false` for me, which is correct. – Florian Wendelborn Sep 13 '21 at 23:04
  • Sorry, I checked it wrong. – Chiara Ani Sep 15 '21 at 15:26
  • I've run this method against one using `typeof` [(example here)](https://jsfiddle.net/zgexqd14/2/) 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`` – Advena Dec 02 '22 at 11:00
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
Peter
  • 37,042
  • 39
  • 142
  • 198
Oriol
  • 274,082
  • 63
  • 437
  • 513