JS Coding Questions Logo
JS Coding Questions
#400💼 Interview💻 Code

How do you detect primitive or non primitive value type

Advertisement

728x90

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
1var myPrimitive = 30;
2
3var myNonPrimitive = {};
4
5function isPrimitive(val) {
6
7return Object(val) !== val;
8}
9
10isPrimitive(myPrimitive);
11
12isPrimitive(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.

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 57

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
400of476
How do you detect primitive or non primitive value type | JSCodingQuestions.com