Tim Lapinskas
← Back to All Posts

Var myObj = {};

October 14, 2016 · Read time: 3 mins

Objects. Today, I spent a ton of time learning about JavaScript objects. w3schools argues that once you understand objects, you understand JavaScript (we’ll see about that haha). I’m spending quite a bit of extra time on the JavaScript track through The Odin Project because I believe JavaScript is going to be uberrrrr important for any web developer gig that I am lucky to land. So today, I kept going with a second introduction to JavaScript track on Codecademy.

But before I learned about objects, I got the chance to recap on a couple other important topics. I got to review if/else statements, switch statements, and some pretty cool new nuances of arrays. This included heterogeneous, multidimensional , and jagged arrays, which both expand the capabilities that arrays have.

So, let’s move onto objects. I know these posts have been getting long, but reiterating the syntax has been super helpful for me. So here goes.

Codecademy describes objects like this. Just like spoken languages, computer languages have similar concepts. English has nouns to describe things, and verbs to describe actions. In JavaScript, nouns are like numbers, strings, or booleans, and verbs are like functions (computer action / computation). The cooler application is using objects to put information and functions in the same place. Syntax below.

Object Literal Notation

  var emptyObject {};

OR

  var object = {
    key: value,
    key: value
  };
Object Constructor Notation

  var object = new Object ();

Keys: Keys/properties can be added to an object after you’ve created it in two ways.

Example Object

  var myObj = {};

Keys can be added like so

  1. myObj ["name"] = "Charlie";

  2. myObj.name = "Charlie";

Putting this all together, we can use objects to store information and quickly call it back in the console. Example stolen from Codecademy.

  var friends = {};
  friends.bill = {
    firstName: "Bill",
    lastName: "Gates",
    number: "(206) 555-5555",
    address: ['One Microsoft Way','Redmond','WA','98052']
  };

  friends.steve = {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(408) 555-5555",
    address: ['1 Infinite Loop','Cupertino','CA','95014']
  };

There is a bit more code that you can add to this to make it searchable (super cool stuff), but I won’t add that in here for now. If you want to check out the rest of the code, go to 6. Data Structures > Contact List (Interactive Lesson). I’m honestly stoked to learn about objects. It really combines everything that I’ve learned so far and seems like the heart of what helps make JavaScript so powerful. Tomorrow, I’ll keep going with Objects and hopefully get through the rest of the JavaScript tutorial on Codecademy. Looking forward to it!

Post-edit: I was chatting with my dad about programming syntax and how hard it is to remember all the nuances. He laughed and said he still looks up how to write switch statements in Ruby. That definitely made me feel a bit better about everything 🙂

 

Originally published on tlapinsk.wordpress.com.