How to Check if a Variable/Value Is a Primitive in JavaScript?

Check if a variable or value is a primitive (as opposed to an object)

In JavaScript, a value can either be a primitive or an object. Therefore, you can check if a value is a JavaScript primitive (as opposed to being an object) using the following check:

!(value instanceof Object)

You may see value !== Object(value) as a means to check for primitive values on some websites. Please note that this would give you some false positives if you use it directly on a value (as opposed to passing it as a function argument). For example, [] !== Object([]) returns true even though an array is not a primitive.

For your convenience, you could create functions for this check, for example, like so:

// ES6+
const isPrimitive = (value) => !(value instanceof Object);

For versions earlier than ES6, you can use the traditional function syntax instead of an arrow function (as arrow function was introduced in ES6).

Refer to the following examples to see the result of this check with different primitive and object values:

Checking if String Is Primitive or Object

A string literal is a primitive. You can check this using the custom isPrimitive() function:

const foo = 'foo';

console.log(isPrimitive(foo)); // true

When you use String() as a function (or wrapper around a primitive value), it coerces the argument given to it to a string primitive (as shown in the example below):

const numericStr = String(123);

console.log(isPrimitive(numericStr)); // true

This is in contrast to creating an object instance (which is what you get when String is called as a constructor — i.e. with the new keyword):

const numericStr = new String(123);

console.log(isPrimitive(numericStr)); // false

In practice, you should rarely find yourself having to use String as a constructor.

Checking if Number Is Primitive or Object

A number literal is a primitive. You can check this using the custom isPrimitive() function:

const posNum = 12345;
const negNum = -12345;
const nanNum = NaN;

console.log(isPrimitive(posNum)); // true
console.log(isPrimitive(negNum)); // true
console.log(isPrimitive(nanNum)); // true

When you use Number() as a function (or wrapper around a primitive value), it coerces the argument given to it to a number primitive (as shown in the example below):

const posNum = Number('12345');
const negNum = Number('-12345');
const nanNum = Number('NaN');

console.log(isPrimitive(posNum)); // true
console.log(isPrimitive(negNum)); // true
console.log(isPrimitive(nanNum)); // true

This is in contrast to creating an object instance (which is what you get when Number is called as a constructor — i.e. with the new keyword):

const posNum = new Number('12345');
const negNum = new Number('-12345');
const nanNum = new Number('NaN');

console.log(isPrimitive(posNum)); // false
console.log(isPrimitive(negNum)); // false
console.log(isPrimitive(nanNum)); // false

In practice, you should rarely find yourself having to use Number as a constructor.

Checking if BigInt Is Primitive or Object

A bigint literal is a primitive. You can check this using the custom isPrimitive() function:

// ES10+
const posNum = 9007199254740991n;
const negNum = -9007199254740991n;

console.log(isPrimitive(posNum)); // true
console.log(isPrimitive(negNum)); // true

When you use BigInt() as a function (or wrapper around a primitive value), it coerces the argument given to it to a bigint primitive (as shown in the example below):

// ES10+
const posNum = BigInt('9007199254740991');
const negNum = BigInt('-9007199254740991');

console.log(isPrimitive(posNum)); // true
console.log(isPrimitive(negNum)); // true

This is in contrast to creating an object instance (which is what you get when BigInt is called as a constructor — i.e. with the new keyword):

const posNum = new BigInt('9007199254740991');
const negNum = new BigInt('-9007199254740991');

console.log(isPrimitive(posNum)); // false
console.log(isPrimitive(negNum)); // false

In practice, you should rarely find yourself having to use BigInt as a constructor.

Checking if Boolean Is Primitive or Object

A boolean literal (i.e. true or false) is a primitive. You can check this using the custom isPrimitive() function:

console.log(isPrimitive(true)); // true
console.log(isPrimitive(false)); // true

When you use Boolean() as a function (or wrapper around a primitive value), it coerces the argument given to it to a boolean primitive (as shown in the example below):

console.log(isPrimitive(Boolean(true))); // true
console.log(isPrimitive(Boolean(false))); // true

This is in contrast to creating an object instance (which is what you get when Boolean is called as a constructor — i.e. with the new keyword):

console.log(isPrimitive(new Boolean(true))); // false
console.log(isPrimitive(new Boolean(false))); // false

In practice, you should rarely find yourself having to use Boolean as a constructor.

Checking if Symbol Is Primitive or Object

Using the custom isPrimitive() function, you can check if a Symbol is a primitive or not in the following way:

// ES6+
const sym = Symbol();

console.log(isPrimitive(sym)); // true
// ES6+
const sym = Symbol('foo');

console.log(isPrimitive(sym)); // true

Symbol() can only be called as a function, and cannot be constructed with new Symbol().

Checking if null and undefined Are Primitive or Object

Using the custom isPrimitive() function, you can check if null and undefined are a primitive or not in the following way:

console.log(isPrimitive(null)); // true
console.log(isPrimitive(undefined)); // true

Please note that for legacy reasons, typeof null gives you 'object' (as opposed to 'null'). null is actually a primitive type.


Hope you found this post useful. It was published . Please show your love and support by sharing this post.