Javascript objects, for a C# developer

Check out my new course Learn you some Lambda best practice for great good! and learn the best practices for performance, cost, security, resilience, observability and scalability.

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

Liked this article? Support me on Patreon and get direct help from me via a private Slack channel or 1-2-1 mentoring.
Subscribe to my newsletter

Hi, I’m Yan. I’m an AWS Serverless Hero and the author of Production-Ready Serverless.

I specialise in rapidly transitioning teams to serverless and building production-ready services on AWS.

Are you struggling with serverless or need guidance on best practices? Do you want someone to review your architecture and help you avoid costly mistakes down the line? Whatever the case, I’m here to help.

Hire me.

Check out my new podcast Real-World Serverless where I talk with engineers who are building amazing things with serverless technologies and discuss the real-world use cases and challenges they face. If you’re interested in what people are actually doing with serverless and what it’s really like to be working with serverless day-to-day, then this is the podcast for you.

Check out my new course, Learn you some Lambda best practice for great good! In this course, you will learn best practices for working with AWS Lambda in terms of performance, cost, security, scalability, resilience and observability. We will also cover latest features from re:Invent 2019 such as Provisioned Concurrency and Lambda Destinations. Enrol now and start learning!

Check out my video 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. There is something for everyone from beginners to more advanced users looking for design patterns and best practices. Enrol now and start learning!

Are you working with Serverless and looking for expert training to level-up your skills? Or are you looking for a solid foundation to start from? Look no further, register for my Production-Ready Serverless workshop to learn how to build production-grade Serverless applications!

Find a workshop near you