Working with object in Javascript

Working with object in Javascript

Working with object in Javascript

  • JavaScript is designed on a simple object-based paradigm.
  • An object is a collection of properties, and a property is an association between a name (or key) and a value.
  • A property's value can be a function, in which case the property is known as a method.
  • We use javascript objects for representing real world objects like person, car, bus, etc.

creating an object in javascript

To create an object with properties, we you use the key:value within the curly braces. For example, the following creates a new person object:


let obj1 = {key: val}
// or
let obj2 = new Object()
obj2.key = val

create a person object

For a person object we have simple attributes firstName, lastName

let john = {
    firstName: "John",
    lastName: "Doe",

let anji = new Object()
    anji.firstName = "Anji"
    anji.lastName = "Batta"

We have created two objects in the above code. 1. john and 2. anji

reading data from javascript object

  • We can do this in 2 ways
  • dot notation
  • square backet notation
let john = {
    firstName: "John",
    lastName: "Doe",

let firstName = john["firstName"];
let lastName = john.lastName;
// John
// Doe

about javascript objects

  • Objects are the foundation of JavaScript and permeate its every aspect.
  • Almost everything in JavaScript is an object.
  • In fact, only six things are not objects. They are — null,undefined, strings, numbers, boolean, and symbols. These are called primitive values or primitive types.
  • Anything that is not a primitive value is an Object.
  • That includes arrays, functions, constructors, and objects themselves.

using functions with objects

  • we can use functions as a keys in javascript object. but, we call it as methods.
  • let's add fullName and greeting methods to person object.
let john = {
    firstName: "John",
    lastName: "Doe",
    fullName: function(){
        return this.firstName + " " + this.lastName;
    greeting: function(){
        console.log("Hello " + this.fullName())

// Hello John Doe