Chapter 4 - Objects

The next type in JS, which isn't a primitive, is Object.

Objects, unlike their counterparts are natively mutable and are considered to be just a collection of properties. Think of our previous example of Charlie and Peleg; those would've been considered objects if we gave them a representation in JS and shortly we will see how.

As we've mentioned before, objects in JS have unique attributes and behaviours. Peleg the human has certain attributes that aren't true about Charlie the dog. For Peleg, he speaks English, whereas Charlie just barks (mostly whines). So this can be considered analogous to the different objects in JS: A String object, for example, has a built in property in it which contains its length. Kindly enough, that property is also called length and it can be accessed like this:

"This is just a sample string".length;

The value yielded from executing this command will be the number 28 (count the number of characters in the sentence).

Side note to the curious: The string we're talking about here is a string object and not a primitive string as we've covered before. JS has an object representation to some of its primitives as well, and it simply switches between the two when it needs to. So when we try to do an "objecty" thing with a primitive, like get its length property, it will convert it to its object representation. How cool! *nerd glance*

Now if you were crazy and tried to get the length of a Number type like this:


You would get undefined. The reason for that is that Numbers don't have the built-in property length in them and therefore the interpreter (read: computer) will print out undefined which is used to represent no value at all.

Side note to the curious II: In the example above, the reason we wrapped the number six in parentheses was to avoid confusing the interpreter into thinking the dot after the number is used to represent a decimal number.

Now before we continue, I want you to know that all of this stuff we've just covered is pretty high-level theoretical nonsense. Yes, it is important that you know it, but will it change much if you didn't get any of it at this point? Not really. The stuff we're going to cover in the rest of this book is going to be a lot more hands-on and less confusing.

And we're back!

Remember I was blabbing about objects and how they are basically a collection of properties? Either way, an object is a collection of properties and it can be declared like this:

var imAnObject = { };

These fancy looking curly braces are the literal way of declaring objects in JS. In other languages they are different. This notation is called JSON and it stands for JavaScript Object Notation. It is used for a bunch of other stuff in computers, and not just in JS, simply because it's ridiculously convenient for representation of data.

Now imAnObject is an object (its type is Object), and to make sure this is really the case, JS offers a special operator by the name of typeof:

typeof imAnObject;

Which will yield the string "object". So now you can sleep soundly at night; you've got yourself an object.

Back to our lovely duo, Peleg and Charlie. When you try this in your Console, make sure to include the colons and commas just like they are placed below. Otherwise you will get an error:

var peleg = {
  name: 'Peleg',
  legs: 2

var charlie = {
  name: 'Charlie',
  legs: 4

Now we have two objects to represent Peleg and Charlie.

If you wanted to access the name property in one of these object, you'd do:;



will print 'Peleg'.

For convenience, we can also access our objects' properties using the bracket notation and a string, like this:


and this will yield the same exact thing.

Now let's say you wanted to mess with us and change the name of peleg and assign a different value to the name property: = "douchebag";

So the name property of peleg will now contain the string "douchebag".

What if you wanted to add a new property to charlie such as lastFedTime:

charlie.lastFedTime = '12:43pm';

You got it, boss.

Go get trashed and hookup with a stranger. Come back tomorrow. You've earned it.

Table of Contents