2

From my understanding: -primitive types tests for equality

'lol' === 'lol'
true

True because content is the same or true because object ID's in memory are the same?

-Composite types tests for identity

[1,2,3] === [1,2,3]
false

False because reference vars are diff or diff because object ID's in memory are diff?

I want to be able to differentiate between:
A) The reference variable
B) The identifier (namespace)
C) The actual object in memory
D) The ID

Are there commands that allow me to test for these things individually?

chopper draw lion4
  • 12,401
  • 13
  • 53
  • 100

2 Answers2

6

'lol'==='lol' gives you true because its a string literal.It falls under the pass by value category and locations are same in the memory.

But if you do something like

var s1=new String('lol');
var s2=new String('lol');
s1===s2 //returns false

because s1 and s2 are string objects and falls into pass by reference category. Values are pointing to different locations in the memory

[1,2,3]===[1,2,3] also returns you false because arrays are by default objects in Javascript. There is no primitive form of an Array in JS. They falls under pass by reference category and memory locations are different.

Ankur Aggarwal
  • 2,993
  • 5
  • 30
  • 56
  • So you seem to be saying that '===' tests for *memory location*, not value equality. Is this true for all data types? – chopper draw lion4 Apr 11 '14 at 19:29
  • == check for equality while === checks for equality and identity. Yes its valid for all – Ankur Aggarwal Apr 11 '14 at 19:30
  • Then how come: a = [1]; b = [1]; a==b false a===b false I would expect the first test to be true and second to be false. Is this diff because it is a composite type? – chopper draw lion4 Apr 11 '14 at 19:33
  • 1
    == do not work with objects.While dealing with objects both '==' and '===' has the same meaning.'==' works with primitive only. '==' will do the type conversion if value is same. so "5"==5 will return you true. while '===' will not type convert it. '5'===5 will return you false. – Ankur Aggarwal Apr 11 '14 at 19:40
4

JavaScript has two kinds of comparison — strict and abstract. The == operator does an abstract comparison, which means it might convert a value to a different type and then compare the resulting values. The === operator does a strict comparison, which means it will never convert types. The way we determine whether two values are strictly equal depends on the type of the values, and not just their location in memory. I looked this up to make sure I got all the cases right, so here's the list from Mozilla's Comparison Operators article.

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two distinct objects are never equal for either strictly or abstract comparisons.
  • An expression comparing Objects is only true if the operands reference the same Object.
  • Null and Undefined Types are == (but not ===).

So the two strings are strictly equal because they contain the exact same sequence of characters. The two arrays are not equal (either strictly or abstractly) because they are two distinct objects.

There is no concept in JavaScript of an "ID", either for primitives or objects. These equality rules are what produce the behavior you see.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • I think you are incorrect mate. == checks for identity, not equality according to http://eloquentjavascript.net/chapter4.html, ""JavaScript's == operator, when comparing objects, will only return true if both values given to it are the precise same value. Comparing different objects with identical contents will give false"" – chopper draw lion4 Apr 11 '14 at 20:29
  • 1
    +1 concise and well written. Here's a reference - strict http://es5.github.io/#x11.9.6 and abstract http://es5.github.io/#x11.9.3 – Benjamin Gruenbaum Apr 11 '14 at 20:30
  • 1
    @chopperdrawlion4 `==` only checks for identity in objects which are anything except null, undefined, number, string and boolean in JavaScript (in ES5). This is what this answer says and I quote "An expression comparing Objects is only true if the operands reference the same Object.". What this answer says and what Eloquent JavaScript says in chapter 4 are essentially the same. If you're unsure about anything, you can always check the language specification which I cited in my last comment. – Benjamin Gruenbaum Apr 11 '14 at 20:31