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 8393061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:41:19+00:00 2026-06-09T19:41:19+00:00

Possible Duplicate: Why does (“foo” === new String(“foo”)) evaluate to false in JavaScript? Over

  • 0

Possible Duplicate:
Why does (“foo” === new String(“foo”)) evaluate to false in JavaScript?

Over here I caught the advice that it’s best to use non type-coercive string comparison, but in Chrome, I discovered something kind of odd:

var t1 = String("Hello world!");
var t2 = new String("Hello world!");
var b1 = (t1==t2); // true
var b2 = (t1===t2); // false

Is this standard behavior? If so, what are the respective types of t1 and t2? Thanks.

  • 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-06-09T19:41:20+00:00Added an answer on June 9, 2026 at 7:41 pm

    String, called as a function, converts its argument to a string. String, called as a constructor, creates an object whose prototype is the String function. (Check James’s Answer for the relevant ECMAScript specification section.)

    This is indeed confusing.

    The two equality operators actually do very different things. From the ECMA-262, v 5.1 document, === does:

    1. If Type(x) is different from Type(y), return false.
    2. If Type(x) is Undefined, return true.
    3. If Type(x) is Null, return true.
    4. If Type(x) is Number, then
      a. If x is NaN, return false.
      b. If y is NaN, return false.
      c. If x is the same Number value as y, return true.
      d. If x is +0 and y is -0, return true.
      e. If x is -0 and y is +0, return true.
      f. Return false.
    5. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
    6. If Type(x) is Boolean, return true if x and y are both true or both false; otherwise, return false.
    7. Return true if x and y refer to the same object. Otherwise, return false.

    Whereas == does:

    1. If Type(x) is the same as Type(y), then
      a. If Type(x) is Undefined, return true.
      b. If Type(x) is Null, return true.
      c. If Type(x) is Number, then
         i. If x is NaN, return false.
         ii. If y is NaN, return false.
         iii. If x is the same Number value as y, return true.
         iv. If x is +0 and y is -0, return true.
         v. If x is -0 and y is +0, return true.
         vi. Return false.
      d. If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
      e. If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
      f. Return true if x and y refer to the same object. Otherwise, return false.
    2. If x is null and y is undefined, return true.
    3. If x is undefined and y is null, return true.
    4. If Type(x) is Number and Type(y) is String, return the result of the comparison
      x == ToNumber(y).
    5. If Type(x) is String and Type(y) is Number, return the result of the comparison
      ToNumber(x) == y.
    6. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
    7. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
    8. If Type(x) is either String or Number and Type(y) is Object, return the result of the comparison x == ToPrimitive(y).
    9. If Type(x) is Object and Type(y) is either String or Number, return the result of the comparison ToPrimitive(x) == y.
    10. Return false.

    Note that in the spec, the Type of a primitive string object is String, whereas the type of any object (including the String object) is Object.

    With === the relevant line is #1: the Type of the objects are different, so false is returned.

    With == the relevant line is #8: x is a String ("Hello world!") and y is an Object (The String object containing the string "Hello world!"). Thus the comparison x == ToPrimitive(y) is made. ToPrimitive ends up calling the valueOf method of the object, or if that method doesn’t exist, the toString method. In this case, a String object‘s valueOf method returns the primitive string the object contains. Thus the equality operation is done again, this time between two primitive strings which contain the same text, which returns true thanks to #1.d.

    JavaScript is a bit messy under the hood…


    EDIT: Notice that if two objects are compared, no conversions apply, but rather, rule #1.f applies. Thus, thanks to the spec, I was able to correctly predict the output of the following code:

    > new String("hi") == new String("hi")
    false
    

    EDIT: Just thought I’d add that these distinctions are even further blurred by more implicit type conversion. For example, the following works:

    > ("hi").toString()
    "hi"
    

    but that’s not because "hi" is an object (like in Python):

    > typeof "hi"
    "string"
    

    But rather, because the . operator does a conversion from the primitive string type to the string Object type (creating a new string object) whose toString method is then called.

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

Sidebar

Related Questions

Possible Duplicate: What does “(void) new” mean in C++? I'm not familiar with C++
Possible Duplicate: JavaScript === vs == : Does it matter which “equal” operator I
Possible Duplicate: JavaScript losing “this” object reference with private/public properties Why does the second
Possible Duplicate: C++'s “placement new” in the below code what does Line 3 represents,
Possible Duplicate: Javascript === vs == : Does it matter which “equal” operator I
Possible Duplicate: What does “options = options || {}” mean in Javascript? I stumbled
Possible Duplicate: Why does gcc have “â” in all its error messages? New to
Possible Duplicate: Javascript === vs == : Does it matter which “equal” operator I
Possible Duplicate: Javascript === vs == : Does it matter which “equal” operator I
Possible Duplicate: Question on this JavaScript Syntax (“What Does This Do?”) What is the

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.