6

I've looked all the questions and answers on stackoverflow, but couldn't find the simple answer to this.

What is exactly the difference between string and object?

For example, if I have this code:

var a = 'Tim';
var b = new String('Tim');

What exactly is the difference?

I understand that new complicates the code, and new String slows it down.

Also, I understand a==b is true, but going more strictly a===b is false. Why?

I seem to fail to understand the process behind the object and string creation. For example:

var a = new String ('Tim');
var b = new String ('Tim');

a==b is false

super11
  • 436
  • 7
  • 19
  • The difference is `"test"` is a string and `new String("test")` is an object. That's why `"test" !== new String("test")`. `new String("test")` is a wrapper object around the string. – dfsq May 11 '16 at 08:59
  • Got it. Could you please post the answer on the logic of object and string creation in more details? Thanks. – super11 May 11 '16 at 09:00
  • `a == b` is `true` because the `==` operator will coerce the types to match (as best it can), whereas the `===` operator is used where you explicitly want to compare the types are they are. – Rory McCrossan May 11 '16 at 09:05

3 Answers3

6

a is of type string, whereas b is of type object.

=== includes typechecking and cause string is not an object a === b will give you a false

new String ('Tim') === new String ('Tim') will evaluate to false too, because both are different objects

chresse
  • 5,486
  • 3
  • 30
  • 47
  • 1
    And even `new String ('Tim') == new String ('Tim')` (loose comparison) is false, because the object references are compared. Even though the objects contain similar data, they are different objects. There is no deep comparison of the properties of the objects, either in loose or strict comparison. – GolezTrol May 11 '16 at 09:03
  • Thanks. Why using `new` slows down the process? Because of object creation? – super11 May 11 '16 at 09:03
  • 2
    @super11 Yes, because of object creation, but also because of its use. An object wrapper around a string is just slightly slower than a 'simple' buffer of characters, although the difference is so small that it should hardly ever be a reason to base your decision on. Use what you think works best for you. – GolezTrol May 11 '16 at 09:05
  • 1
    From WC3: JavaScript objects cannot be compared. Thanks. – super11 May 11 '16 at 09:06
3

For normal strings there is no need to create an object, just create your variable and assign it a value.

And as far as your question regarding why == is true and === is false it's because:

== Compares values === Compares values AND type (One is a string, one is an object).

Another example of this is:

var a = 1;

var b = '1';

a == b //True as they both have the same value

a === b //false as one is a string and one is an integer

1

You can do the following to see the difference:

var a = "foo";
var b = new String("foo");
console.log(a);
console.log(b);

enter image description here

The first one is a string literal and the second is a String object. That's why when you compare them they are not equal but when you compare their values they are. You can read more about literals here.

Dropout
  • 13,653
  • 10
  • 56
  • 109