How to check if the value is primitive or not in JavaScript ?
Last Updated :
29 May, 2024
To check if the value is primitive we have to compare the value data type. As Object is the non-primitive data type in JavaScript we can compare the value type to object and get the required results.
Primitive data types are basic building blocks like numbers and characters, while non-primitive data types, such as arrays and objects, are more complex and can store multiple values or structures.
- Primitive data types − String, number, undefined, boolean, null, symbols, bigint.
- Non-primitive data types − Object
When we access a primitive value, we manipulate the actual value stored in that variable. Thus, variables that are primitive are accessed by value. When we assign a variable that stores a primitive value to another, the value stored in the variable is created and copied into the new variable.
To check a value whether it is primitive or not we use the following approaches:
If the type of the value is 'object' or 'function' then the value is not primitive otherwise the value is primitive. But the typeof operator shows the null to be an "object" but actually, the null is a Primitive.
Example: In this example, we are using the typeof operator
JavaScript
let isPrimitive = (val) => {
if(val === null){
console.log(true);
return;
}
if(typeof val == "object" || typeof val == "function"){
console.log(false)
}else{
console.log(true)
}
}
isPrimitive(null)
isPrimitive(12)
isPrimitive(Number(12))
isPrimitive("Hello world")
isPrimitive(new String("Hello world"))
isPrimitive(true)
isPrimitive([])
isPrimitive({})
Outputtrue
true
true
true
false
true
false
false
Using the Object()
In this we check if the value is equal to Object(value). If the value is equal then the value is primitive otherwise not primitive.
Syntax
Value !== Object(inputValue);
Example: In this example, we are using Object().
JavaScript
let isPrimitive = (val) => {
if(val === Object(val)){
console.log(false)
}else{
console.log(true)
}
}
isPrimitive(null)
isPrimitive(12)
isPrimitive(Number(12))
isPrimitive("Hello world")
isPrimitive(new String("Hello world"))
isPrimitive(true)
isPrimitive([])
isPrimitive({})
Outputtrue
true
true
true
false
true
false
false
Using the instanceof operator
The instanceof operator checks whether an object belongs to a specific class or not. While it's typically used to check if an object is an instance of a particular constructor function, it can also be used to differentiate between primitive and non-primitive values.
Example:
JavaScript
function isPrimitive(value) {
return (
value instanceof Number ||
value instanceof String ||
value instanceof Boolean ||
value instanceof Symbol ||
value instanceof BigInt
);
}
console.log(isPrimitive(null));
console.log(isPrimitive(12));
console.log(isPrimitive(Number(12)));
console.log(isPrimitive("Hello world"));
console.log(isPrimitive(new String("Hello world")));
console.log(isPrimitive(true));
console.log(isPrimitive([]));
console.log(isPrimitive({}));
Outputfalse
false
false
false
true
false
false
false
Similar Reads
How to check if the provided value is of the specified type in JavaScript ? To check if the provided value is of the specified type in JavaScript, we have multiple approaches. Below are the approaches used to check if the provided value is of the specified type in JavaScript: Table of Content Using Object.is() Using TypeOf OperatorApproach: Using Object.is() Object.is() det
3 min read
How to Check if a Value is a Number in JavaScript ? To check if a value is a number in JavaScript, use the typeof operator to ensure the value's type is 'number'. Additionally, functions like Number.isFinite() and !isNaN() can verify if a value is a valid, finite number.Methods to Check if a Value is a NumberThere are various ways to check if a value
3 min read
How to check if a value is object-like in JavaScript ? In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 min read
How to check if a Variable Is Not Null in JavaScript ? In JavaScript, checking if a variable is not null ensures that the variable has been assigned a value and is not empty or uninitialized. This check is important for avoiding errors when accessing or manipulating data, ensuring that the variable holds valid, usable content before proceeding with oper
4 min read
How to Check if Object is JSON in JavaScript ? JSON is used to store and exchange data in a structured format. To check if an object is JSON in JavaScript, you can use various approaches and methods. There are several possible approaches to check if the object is JSON in JavaScript which are as follows: Table of Content Using Constructor Type Ch
2 min read
How to check the type of a variable or object in JavaScript ? In this article, How to Check the Type of a Variable or Object in JavaScript? In JavaScript, the typeof operator is used to determine the typeof an object or variable. JavaScript, on the other hand, is a dynamically typed (or weakly typed) language. This indicates that a variable can have any type o
2 min read