InterviewHubPRO
Section Overview
Q404

Question 404 of 521

Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

78%
JavaScript Internals (521 Qs)•Part 7: Execution Context, Functional JS & V8 Engine (404 - 463)

How do you detect primitive or non primitive value type

In JavaScript, primitive types include boolean, string, number, BigInt, null, Symbol and undefined. Whereas non-primitive types include the Objects. But you can easily identify them with the below function,

javascript
 var myPrimitive = 30;
 var myNonPrimitive = {};
 function isPrimitive(val) {
   return Object(val) !== val;
 }

 isPrimitive(myPrimitive);
 isPrimitive(myNonPrimitive);

If the value is a primitive data type, the Object constructor creates a new wrapper object for the value. But If the value is a non-primitive data type (an object), the Object constructor will give the same object.