JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 10 Objects

About Objects

Arrays are great for lists, but cannot hold a lot of information about one thing. Consider the following array of cats.

var myCatArray = ["Gideon", "Zelda", "Misty", "Pepper"];

What if we wanted to know more information about these cats besides just their names? Let's say we wanted to know about the breed, the size, the color, their favorite toys, etc. We could create multiple arrays, but then it's difficult to know more information about each cat because they're in different places. Also, if anything changed, and the information got reorganized then you wouldn't be able to get the different info for each cat.

Instead, when we want to have a bunch of information together about one thing, we can use an object. Consider the following objects, now we know multiple things about each cat stored in one place.

var gideon = { name: "Zelda", size: "small", color: "brown", favToy: "hair ties"}
var zelda = { name: "Gideon", size: "large", color: "orange", favToy: "cat nip carrot"}

What's the advantage of doing it this way? Well, now if we want all the information about Gideon, we can just use the gideon variable, and have access to all of his info in one place!

You can think of Objects like keys on a keyring. Each key is for a specific lock and if you have nice identifiers on each key, it's easy to use them to get what you want. In javascript, everything on the left hand side of the : are keys (or properties) and everything on the right side are values. So it's key: value. In our above example the keys are name, size, color, favToy, and the values are what is to the right of the :.

Objects are often used in conjunction with arrays, to make an array of objects. So arrays hold order, and then the objects can hold all of the info. So you could do the following.

var cats = [
    { name: "Zelda", size: "small", color: "brown", favToy: "hair ties"},
    { name: "Gideon", size: "large", color: "orange", favToy: "cat nip carrot"}
]

Next, we'll talk more in depth about objects, know that you know why you might want to use them.

Previous10 ObjectsNextProperties

Last updated 7 years ago