One can use typeof
to determine whether a value is primitive or boxed.
Consider:
typeof "foo"; // "string"
typeof new String("foo"); // "object"
In combination with Object.prototype.toString
we could define the following two functions
var toString = Object.prototype.toString;
var is_primitive_string = function(s) {
return toString.call(s) === "[object String]" && typeof s === "string";
};
var is_boxed_string = function(s) {
return toString.call(s) === "[object String]" && typeof s === "object";
};
Are there any use cases for these two functions? (Or similar functions for Number
, Boolean
, etc).
The concept behind this question came from the following Comment by T.J.Crowder.
Should we ever care whether a value we have is primitive or boxed?