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

  • SEARCH
  • Home
  • 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 3235058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:27:47+00:00 2026-05-17T17:27:47+00:00

Javascript has lot’s of tricks around types and type conversions so I’m wondering if

  • 0

Javascript has lot’s of “tricks” around types and type conversions so I’m wondering if these 2 methods are the same or if there is some corner case that makes them different?

  • 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-17T17:27:47+00:00Added an answer on May 17, 2026 at 5:27 pm

    They are not completely the same, and actually, the String constructor called as a function (your first example), will at the end, call the toString method of the object passed, for example:

    var o = { toString: function () { return "foo"; } };
    String(o); // "foo"
    

    On the other hand, if an identifier refers to null or undefined, you can’t use the toString method, it will give you a TypeError exception:

    var value = null;
    String(null);     // "null"
    value.toString(); // TypeError
    

    The String constructor called as a function would be roughly equivalent to:

    value + '';
    

    The type conversion rules from Object-to-Primitive are detailed described on the specification, the [[DefaultValue]] internal operation.

    Briefly summarized, when converting from Object-to-String, the following steps are taken:

    1. If available, execute the toString method.
      • If the result is a primitive, return result, else go to Step 2.
    2. If available, execute the valueOf method.
      • If the result is a primitive, return result, else go to Step 3.
    3. Throw TypeError.

    Given the above rules, we can make an example of the semantics involved:

    var o = {
      toString: function () { return "foo"; },
      valueOf:  function () { return "bar"; }
    };
    
    String(o); // "foo"
    
    // Make the toString method unavailable:
    o.toString = null;
    
    String(o); // "bar"
    
    // Also make the valueOf method unavailable:
    o.valueOf = null;
    
    try { 
      String(o); 
    } catch (e) {
      alert(e); // TypeError
    }
    

    If you want to know more about this mechanism I would recommend looking at the ToPrimitive and the ToString internal operations.

    I also recommend reading this article:

    • Object-to-Primitive Conversions in JavaScript
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a web application that has a lot to download (javascript, images,
I am writing an application in JavaScript (with JQuery). The application has a lot
The following JavaScript has some unwanted behaviour: <html> <script> function AddEventListener(el, listener) { el.addEventListener
As per http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf , JavaScript has 6 types: undefined , null , boolean ,
I have some third-party Javascript that has statements like this: FOO = function() {
I am currently working on a website which has a lot of javascript .
I have created a JavaScript application that has a lot of array manipulations (sorting,
When I try to debug the javascript code which has a lot of closures
Looking through the Mozilla JavaScript site, I see that JavaScript 1.8 has a lot
My Application has a lot of calculation being done in JavaScript according to how

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.