object - Why does ("foo" === new String("foo")) evaluate to false in JavaScript? -


i going start using === (triple equals, strict comparison) time when comparing string values, find

"foo" === new string("foo") 

is false, , same this:

var f = "foo", g = new string("foo"); f === g; // false 

of course:

f == g; // true 

so recommended use == string comparison, or convert variables strings before comparing?

"foo" string primitive. (this concept not exist in c# or java)

new string("foo") boxed string object.

the === operator behaves differently on primitives , objects.
when comparing primitives (of same type), === return true if both have same value.

when comparing objects, === return true if refer same object (comparing reference). thus, new string("a") !== new string("a").

in case, === returns false because operands of different types (one primitive , other object).


primitives not objects @ all.
typeof operator not return "object" primitives.

when try access property of primitive (using object), javascript language box object, creating new object every time. described in specification.

this why cannot put properties on primitives:

var x = "a"; x.property = 2; alert(x.property) //undefined 

each time write x.property, different boxed string object created.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -