Skip to main content

Command Palette

Search for a command to run...

Object

Updated
•3 min read•View as Markdown
Object
D

Hi, I’m Divyanshi, a who loves building products. I’d love to combine my passion for learning with my software development skills to continue building personalized learning products for people.I am systematically organized, meticulous, diligent and conscientious self-starter, able to prioritize, plan and strategize effectively to accomplish multiple tasks and stay calm under pressure.With my prior knowledge and experience of project management and timed-completion, I move forward to discover and learn diversified challenges in the field of technology that just makes me learn more. Due to the diversity of our clients which included technology corporation,medical industry,foundations,I learn to adapt to changing environments,quickly learning new content & collaborate with people with different communication skills. Skills:Html, Css, Jquery, Javascript, Express, Node.js, Webpack, Basic AEM Fundamentals

The typeof operator will return a string that tells you the data type.

var college={};
typeof college
"object"
typeof "hello" 
// returns "string"
typeof true 
// returns "boolean"
typeof [1, 2, 3] 
// returns "object" (Arrays are a type of object)
typeof function hello() { } 
// returns "function"

Primitive Data types- strings, number, booleans, undefined and null.

Object

Object have properties and things they can do. To add this information , you can define key value pairs for each piece of data. Objects are data structure in JavaScript that lets you store data about a particular thing, and helps you keep track of that data by using a "key". Objects allow you group the information into meaningful structure.

var college ={
location="Delhi"
}

Object-literal notation

var sister = {
  name: "Sarah", 
  age: 23,
  parents: [ "alice", "andy" ],
  siblings: ["julia"],
  favoriteColor: "purple",
  pets: true
};

The syntax you see above is called object-literal notation. There are some important things you need to remember when you're structuring an object literal:

  • The "key" (representing a property or method name) and its "value" are separated from each other by a colon
  • The key: value pairs are separated from each other by commas
  • The entire object is wrapped inside curly braces { }.

The key in a key:value pair allows you to look up a piece of information about an object. Here's are a couple examples of how you can retrieve information about my sister's parents using the object you created.

// two equivalent ways to use the key to return its value
sister["parents"] // returns [ "alice", "andy" ]
sister.parents // also returns ["alice", "andy"]

Using sister["parents"] is called bracket notation (because of the brackets!) and using sister.parents is called dot notation (because of the dot!).

What about methods?

var sister = {
  name: "Sarah",
  age: 23,
  parents: [ "alice", "andy" ],
  siblings: ["julia"],
  favoriteColor: "purple",
  pets: true,
  paintPicture: function() { return "Sarah paints!"; }
};

sister.paintPicture();
Returns: "Sarah paints!"
//and you can access the name of my sister by accessing the name property:
sister.name
Returns: 
"Sarah"

Naming conventions

Feel free to use upper and lowercase numbers and letters, but don't start your property name with a number. You don't need to wrap the string in quotes! If it's a multi-word property, use camel case. Don't use hyphens in your property names

var richard = {
  "1stSon": true;
  "loves-snow": true;
};

richard.1stSon // error
richard.loves-snow // error

You can use ["bracket notation"] or .dotNotation to access properties and methods from an object. And, with function expressions, you only need to use the variable name where the function is stored to call the function.

Directions:
Using the object from the previous quiz, answer the following quiz question:
var savingsAccount = {
  balance: 1000,
  interestRatePercent: 1,
  deposit: function addMoney(amount) {
    if (amount > 0) {
      savingsAccount.balance += amount;
    }
  },
  withdraw: function removeMoney(amount) {
    var verifyBalance = savingsAccount.balance - amount;
    if (amount > 0 && verifyBalance >= 0) {
      savingsAccount.balance -= amount;
    }
  }
};
Which of the following are valid ways to access properties and call methods from the savingsAccount object?
savingsAccount.balance;
savingsAccount["balance"];
savingsAccount.withdraw(50);

More from this blog