Results 1 to 5 of 5

Thread: [RESOLVED] Don't understand these two statements?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Resolved [RESOLVED] Don't understand these two statements?

    I'm going thru the Javascript tutorial on W3Schools.com and came across something that I really don't quite understand. An understandable explanation was not given in the tutorial. So I was hoping someone could explain this to me.

    The scenario is this

    var x = new String("John")
    var y = new String("John")

    The following statement is false and I don't know why.

    (x == y)

    I understand the comparison of types and objects using the "===" operator but the above two statements appear to have the same objects even though (x == y) says otherwise.

    I'm confused.

    Thanks,
    Blake

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Don't understand these two statements?

    String DOES NOT create a primitive string - String() creates a STRING object - which is a factory of sorts for working with strings.

    This would work for equality

    new String("a").valueOf() == new String("a").valueOf()

    Odd they didn't explain this further. It's hard enough to get around the =, == and === operators in JS, no reason to add this oddity as well to that mix!

    Code:
    "A" == "A"
    true
    "A" === "A"
    true
    (new String("A") == new String("A"))
    false
    (new String("A") === new String("A"))
    false
    (new String("A").valueOf() === new String("A").valueOf())
    true

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Don't understand these two statements?

    Check out how a WATCH on a literal string of "AB" is a primitive value.

    And note how the WATCH on the "new String("AB")" returns a complex object with many methods attached,
    Attached Images Attached Images  

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Don't understand these two statements?

    This is a helpful link

    http://clarkfeusier.com/2015/03/19/g...avascript.html

    btw - you have officially scared me into looking at where I might use String()

    For example - if I have a known numeric value in a variable - let's say: X = 5;

    When I do this: strX = String(X);

    I am not getting a simple string with the value of "5" - it's returning this complex object.

    What I really should always do is this: strX = String(X).valueOf();

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    5,034

    Re: Don't understand these two statements?

    szlmany,

    Thanks for the explanation and illustrations. I guess the important take-away is to use the "valueOf()" method when assigning/retrieving string values.
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width