Javascript Data Types

Lets take a quick look at javascript data types because there is a difference in behaviour when it comes to variable comparisons.

Data Types

Ok, the easy bit...there are 2 data types, Primitive Values and Objects. Easy.

Here are the primitives:

  • booleans
  • strings
  • numbers
  • null
  • undefined

...which means everything else is:

  • an object

Primitive Comparisons

Here's an example comparison between 2 primitive value variables that are initialised identically then compared:

var myNumber1 = 128;
var myNumber2 = 128;
myNumber1 === myNumber2;
true;

Object Comparisons

Whereas if the variables are object values initialised identically and compared:

var myObject1 = {};
var myObject2 = {};
myObject1 === myObject2;
false;

Conclusion

Every object value has its own unique id and it is this reference id that is compared - hence the result is always false. So its important to understand the data value type to know what result to expect when comparing variables.