DEV Community

Cover image for Objects in JS
Vigneshwaran V
Vigneshwaran V

Posted on

Objects in JS

Object

An object in JavaScript is a standalone data structure that stores data as a collection of key-value pairs, where each key (property name) maps to a specific value. Unlike primitive data types that hold a single value, objects group related data and functionalities together.

  • An object is a dynamic data structure that stores related data as key-value pairs, where each key uniquely identifies its value.

  • The values of properties can be primitives, objects, or functions (known as methods when defined inside an object).

  • Objects are mutable and dynamic properties can be added, modified, or deleted at any time.

  • Objects allow data grouping and encapsulation, making it easier to manage related information and behaviour together.

There are two primary ways to create an object in JavaScript:

1. Creation Using Object Literal
The object literal syntax allows you to define and initialize an object with curly braces {}, setting properties as key-value pairs.

let obj = {
    name: "Sourav",
    age: 23,
    job: "Developer"
};
console.log(obj);
Enter fullscreen mode Exit fullscreen mode

2. Creation Using new Object() Constructor

let obj = new Object();
obj.name= "Sourav",
obj.age= 23,
obj.job= "Developer"

console.log(obj);
Enter fullscreen mode Exit fullscreen mode

Accessing Object Properties

const person = {
  name: "John",
  age: 25,
  city: "New York"
};

console.log(person);
Enter fullscreen mode Exit fullscreen mode

Dot Notation

console.log(person.name); // John
Enter fullscreen mode Exit fullscreen mode

Bracket Notation

console.log(person["age"]); // 25
Enter fullscreen mode Exit fullscreen mode

Adding Properties

person.email = "john@example.com";
person["country"] = "USA";
Enter fullscreen mode Exit fullscreen mode

Updating Properties

person.age = 26;
Enter fullscreen mode Exit fullscreen mode

Deleting Properties

delete person.city;
Enter fullscreen mode Exit fullscreen mode

Object Methods

An object method is a function stored inside an object.

Example

const student = {
    name: "Vicky",
    age: 21,

    greet: function() {
        console.log("Hello");
    }
};

student.greet();
Enter fullscreen mode Exit fullscreen mode

Output

Hello
Enter fullscreen mode Exit fullscreen mode

Here name and age are properties, greet() is a method.

Why use methods?

Methods allow an object to perform actions.

const calculator = {
    add: function(a, b) {
        return a + b;
    }
};

console.log(calculator.add(10, 20));
Enter fullscreen mode Exit fullscreen mode

Output

30
Enter fullscreen mode Exit fullscreen mode

Useful Object Methods

Object.keys()
Returns all property names (keys).

const student = {
    name: "Vicky",
    age: 21
};

console.log(Object.keys(student));
Enter fullscreen mode Exit fullscreen mode

Output

["name", "age"]
Enter fullscreen mode Exit fullscreen mode

Object.values()
Returns all values.

console.log(Object.values(student));
Enter fullscreen mode Exit fullscreen mode

Output

["Vicky", 21]
Enter fullscreen mode Exit fullscreen mode

Object.entries()
Returns both keys and values as arrays.

console.log(Object.entries(student));
Enter fullscreen mode Exit fullscreen mode

Output

[
  ["name", "Vicky"],
  ["age", 21]
]
Enter fullscreen mode Exit fullscreen mode

If you want to check whether an object contains a particular property, there are several ways.

1. Using in Operator (Recommended)

const student = {
    name: "Vicky",
    age: 21
};

console.log("name" in student); // true
console.log("city" in student); // false
Enter fullscreen mode Exit fullscreen mode

Output

true
false
Enter fullscreen mode Exit fullscreen mode

2. Using hasOwnProperty()

const student = {
    name: "Vicky",
    age: 21
};

console.log(student.hasOwnProperty("name")); // true
console.log(student.hasOwnProperty("city")); // false
Enter fullscreen mode Exit fullscreen mode

Output

true
false
Enter fullscreen mode Exit fullscreen mode

If you want to check whether an object contains a particular value, use Object.values() along with includes().

Example

const student = {
    name: "Vicky",
    age: 21,
    dept: "CSE"
};

console.log(Object.values(student).includes("Vicky")); // true
console.log(Object.values(student).includes("ECE"));   // false
Enter fullscreen mode Exit fullscreen mode

Output

true
false
Enter fullscreen mode Exit fullscreen mode

Reference

https://www.geeksforgeeks.org/javascript/objects-in-javascript/

Top comments (0)