Javascript objects, for a C# developer

Com­ing from a C# back­ground, the thing that has struck me the most in learn­ing javascript is its com­bi­na­tion of dynam­ic and loose type sys­tem and pro­toy­pal inher­i­tance mod­el (more on this lat­er).

Objects in javascript are very dif­fer­ent from those in C#, though the ExpandoOb­ject type in C# 4 offers sim­i­lar abil­i­ty to add/remove prop­er­ties dynam­i­cal­ly, javascript’s type sys­tem offers even greater free­dom! Here’s a list of quick bul­let points which hope­ful­ly can help clear some of your con­fu­sions around javascript’s objects and type sys­tem.

Prim­i­tive types

  • There are three prim­i­tive types in javascript – Num­ber, String and Boolean
  • Inte­gers and floats are both of num­ber type
var integer = 8;
var float = 8.3;
typeof(integer) == typeof(float); // true
  • Use the isNaN() glob­al func­tion to see if a val­ue is ‘Not-a-Num­ber’
isNaN(123); // false
isNaN(1.23); // false
isNaN(10-2); // false
isNaN("Hello World"); // true
isNaN("2010/01/04"); // true
  • Use the par­seInt() func­tion to explic­it­ly parse a string to inte­ger, which keeps the inte­ger por­tion of the string
parseInt("8"); // 8
parseInt("8.6"); // 8
  • Use the parse­Float() func­tion to explic­it­ly parse a string to float
parseFloat("8.6"); // 8.6
  • String quote types are inter­change­able, but must match
"Hello World" == 'Hello World'; // true
  • There are no char type in javascript
typeof("Hello World".charAt(1)); // string

Spe­cial cas­es

  • There are two spe­cial cas­es – Null and Unde­fined
var d;
typeof(d); // undefined

var array = ["one", "two", "three"];
array[0]; // one
array[3]; // undefined

Object types

  • Arrays are objects
var array = ["one", "two", "three"];
typeof(array); // object
  • Array items don’t have to be the same type
var array = ["one", 2, false];
array[0]; // one
array[2]; // false
  • Use pop() and push() func­tions to add/remove items from an array like a stack (LIFO)
var array = ["one", "two","three"];
array.pop(); // array = ["one", "two"];
array.push("four"); // array = ["one", "two", "four"];
  • Func­tions are objects
  • An object in javascript is basi­cal­ly a set of name val­ue pairs where the names are strings and the val­ues are strings, num­bers, booleans and objects (includ­ing arrays and func­tions)
  • You can cre­ate a new object by spec­i­fy­ing the name val­ue pairs
var person = { Name: "John", Age: 10 };
person.Name; // John
person.Age; // 10
  • You can dynam­i­cal­ly add prop­er­ty to an object using lit­er­als
var person = {};
person.Name = "John";
person.Age = 10;
person.Name; // John
person.Age; // 10
  • You can dynam­i­cal­ly add prop­er­ty to an object using strings
var person = {};
person["Name"] = "John";
person["Age"] = 10;
person.Name; // John
person.Age; // 10

Type coer­cion

  • Type coer­cion = the type con­ver­sion which hap­pens implic­it­ly
8 + "8"; // 88
8 + 8 + "8"; // 168, conversion happened when the string "8" was encountered
  • Type coer­cion also applies to com­par­isons
8 == "8"; // true
1 == true; // true
  • Pre­vent type coer­cion in com­par­isons by using the === (exact­ly equal) oper­a­tor
8 === "8"; // false
1 === true; // false

So that’s it, a quick run through of some of the things you would want to know about types and objects in javascript hav­ing come from a lan­guage sta­t­ic typ­ing sys­tem such as C# or Java.


Object ori­ent­ed basics of Javascript

Under­stand­ing loose typ­ing in Javascript

Learn­ing Pro­gram­ming Lan­guages with Koans