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
  • 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.


Object oriented basics of Javascript

Understanding loose typing in Javascript

Learning Programming Languages with Koans

Enjoy what you’re reading? Subscribe to my newsletter and get more content on AWS and serverless technologies delivered straight to your inbox.

Yan Cui

I’m an AWS Serverless Hero and the author of Production-Ready Serverless. I have run production workload at scale in AWS for nearly 10 years and I have been an architect or principal engineer with a variety of industries ranging from banking, e-commerce, sports streaming to mobile gaming. I currently work as an independent consultant focused on AWS and serverless.

You can contact me via Email, Twitter and LinkedIn.

Hire me.

Check out my new course, Complete Guide to AWS Step Functions.

In this course, we’ll cover everything you need to know to use AWS Step Functions service effectively. Including basic concepts, HTTP and event triggers, activities, design patterns and best practices.

Get Your Copy