Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
172
rated 0 times [  176] [ 4]  / answers: 1 / hits: 31138  / 9 Years ago, tue, july 21, 2015, 12:00:00

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.


More From » javascript

 Answers
28

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/


[#65734] Saturday, July 18, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylee

Total Points: 60
Total Questions: 119
Total Answers: 101

Location: Bonaire
Member since Wed, Mar 29, 2023
1 Year ago
;