Javascript objects, for a C# developer

Coming from a C# background, the thing that has struck me the most in learning javascript is its combination of dynamic and loose type system and protoypal inheritance model (more on this later).

Objects in javascript are very different from those in C#, though the ExpandoObject type in C# 4 offers similar ability to add/remove properties dynamically, javascript’s type system offers even greater freedom! Here’s a list of quick bullet points which hopefully can help clear some of your confusions around javascript’s objects and type system.

Primitive types

  • There are three primitive types in javascript – Number, String and Boolean
  • Integers and floats are both of number type
var integer = 8;
var float = 8.3;
typeof(integer) == typeof(float); // true
  • Use the isNaN() global function to see if a value is ‘Not-a-Number’
isNaN(123); // false
isNaN(1.23); // false
isNaN(10-2); // false
isNaN("Hello World"); // true
isNaN("2010/01/04"); // true
  • Use the parseInt() function to explicitly parse a string to integer, which keeps the integer portion of the string
parseInt("8"); // 8
parseInt("8.6"); // 8
  • Use the parseFloat() function to explicitly parse a string to float
parseFloat("8.6"); // 8.6
  • String quote types are interchangeable, but must match
"Hello World" == 'Hello World'; // true
  • There are no char type in javascript
typeof("Hello World".charAt(1)); // string

Special cases

  • There are two special cases – Null and Undefined
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
typeof([]);
  • 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() functions 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"];
  • Functions are objects
  • An object in javascript is basically a set of name value pairs where the names are strings and the values are strings, numbers, booleans and objects (including arrays and functions)
  • You can create a new object by specifying the name value pairs
var person = { Name: "John", Age: 10 };
person.Name; // John
person.Age; // 10
  • You can dynamically add property to an object using literals
var person = {};
person.Name = "John";
person.Age = 10;
person.Name; // John
person.Age; // 10
  • You can dynamically add property to an object using strings
var person = {};
person["Name"] = "John";
person["Age"] = 10;
person.Name; // John
person.Age; // 10

Type coercion

  • Type coercion = the type conversion which happens implicitly
8 + "8"; // 88
8 + 8 + "8"; // 168, conversion happened when the string "8" was encountered
  • Type coercion also applies to comparisons
8 == "8"; // true
1 == true; // true
  • Prevent type coercion in comparisons by using the === (exactly equal) operator
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 having come from a language static typing system such as C# or Java.

References:

Object oriented basics of Javascript

Understanding loose typing in Javascript

Learning Programming Languages with Koans