Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6178725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:30:32+00:00 2026-05-24T00:30:32+00:00

One can use typeof to determine whether a value is primitive or boxed. Consider:

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:30:33+00:00Added an answer on May 24, 2026 at 12:30 am

    I’d say there’s virtually no point, you almost never care whether you’re dealing with a string primitive or a String object.

    There are edge cases. For instance, a String object is an actual object, you can add properties to it. This lets you do things like this:

    function test(arg) {
        arg.foo = "bar";
    }
    

    If calling code passes in a string primitive:

    var s1 = "str";
    test(s1);
    

    …arg gets promoted to a String object and gets a property added to it, but that String object isn’t used by anything after test returns.

    In contrast, if calling code passes in a String object:

    var s2 = new String("str");
    test(s2);
    

    …then the property is added to that object and the calling code can see it. Consider (live copy):

    var s1, s2;
    
    s1 = "str";
    
    display("[Before] typeof s1.foo = " + typeof s1.foo);
    test(s1);
    display("[After] typeof s1.foo = " + typeof s1.foo);
    
    s2 = new String("str");
    
    display("[Before] typeof s2.foo = " + typeof s2.foo);
    test(s2);
    display("[After] typeof s2.foo = " + typeof s2.foo);
    
    function test(arg) {
      arg.foo = "bar";
    }
    

    Output:

    [Before] typeof s1.foo = undefined
    [After] typeof s1.foo = undefined
    [Before] typeof s2.foo = undefined
    [After] typeof s2.foo = string

    Note that s2.foo is a string, but s1.foo isn’t (because s1 was a string primitive, the object created when we promoted it in test has nothing to do with the calling code).

    Is there any use case for this? Dunno. I’d say it would be an extremely edgy edge case if so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the highest port number one can use?
What are some useful Oracle optimizations one can use for an Application that mostly
On the MSVC++ compiler, one can use the __int8 , __int16 , __int32 and
You can use more than one css class in an HTML tag in current
Obviously I can use BCP but here is the issue. If one of the
One of the advantages of Flash/Flex is that you can use vector graphics (SVG),
Does any one know of a control that i can use with a ASP.Net
I have one text input and one button (see below). How can I use
I can understand the use for one level of namespaces. But 3 levels of
Can some one specify the windows API, one need to use in order to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.