super11 super11 - 6 months ago 9
Javascript Question

Javascript: String vs. Object

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');


x==y
is
false

Answer

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