Skip to main content

Command Palette

Search for a command to run...

Discover How JavaScript Handles Object Properties and Methods

How JavaScript Deals with Object Properties and Methods

Updated
3 min read
Discover How JavaScript Handles Object Properties and Methods
I

Hello there! My name is Indrajeet, and I am a skilled web developer passionate about creating innovative, user-friendly, and dynamic web applications. 📌My Expertise advanced proficiency in angular front-End Development. Back-End Development Leveraging the power of .NET, I build secure, robust, and scalable server-side architectures.

JavaScript objects form the backbone of programming, encapsulating data and behavior. They consist of properties (attributes) and methods (functions).

This article will walk you through examples, use cases, and best practices for working with objects in JavaScript.


Properties

Properties define the state or attributes of an object. They associate a unique key (property name) with a value. Let’s explore examples to understand properties better.

Example 1: Basic Object Properties

let calculator = {
  add: function(a, b) {
    return a + b;
  },
  multiply: function(a, b) {
    return a * b;
  }
};

console.log(calculator.add(2, 3)); // Output: 5
console.log(calculator.multiply(4, 5)); // Output: 20

Example 2: Using Dynamic Keys

Dynamic keys enable flexibility in defining object properties.

let property = "color";
let item = {
  name: "Chair",
  [property]: "blue"
};

console.log(item.color); // Output: blue

Example 3: Nested Properties

Properties can be nested within objects to represent hierarchical data.

let student = {
  name: "Alice",
  scores: {
    math: 85,
    science: 92
  }
};

console.log(student.scores.math); // Output: 85

Use Cases for Properties

  1. Representing Data: Storing attributes like name, age, or preferences.

  2. Configuring Settings: Defining app settings, e.g., theme: "dark".


Methods

Methods define the behavior or actions of an object. They are functions associated with an object.

Example 1: Object with Methods

let calculator = {
  add: function(a, b) {
    return a + b;
  },
  multiply: function(a, b) {
    return a * b;
  }
};

console.log(calculator.add(2, 3)); // Output: 5
console.log(calculator.multiply(4, 5)); // Output: 20

Example 2: Accessing Properties in Methods

Methods can access properties of their own object using this.

let car = {
  brand: "Tesla",
  speed: 120,
  getDetails: function() {
    return `Brand: ${this.brand}, Speed: ${this.speed}`;
  }
};

console.log(car.getDetails()); // Output: Brand: Tesla, Speed: 120

Example 3: Chaining Methods

Chaining methods allows multiple actions in sequence.

let counter = {
  value: 0,
  increment: function() {
    this.value++;
    return this;
  },
  decrement: function() {
    this.value--;
    return this;
  },
  show: function() {
    console.log(this.value);
    return this;
  }
};

counter.increment().increment().decrement().show(); // Output: 1

Use Cases for Methods

  1. Encapsulating Behavior: Actions like .start() or .calculate().

  2. Interactive UI: Toggling visibility with .toggleVisibility().

  3. APIs: Custom methods like .fetchData() for HTTP requests.


Combining Properties and Methods

Using properties and methods together makes objects powerful and versatile.

Example: Bank Account Management

let bankAccount = {
  accountHolder: "Sarah",
  balance: 1000,
  deposit: function(amount) {
    this.balance += amount;
    return `New balance: ${this.balance}`;
  },
  withdraw: function(amount) {
    if (amount > this.balance) {
      return "Insufficient funds";
    } else {
      this.balance -= amount;
      return `New balance: ${this.balance}`;
    }
  }
};

console.log(bankAccount.deposit(500)); // Output: New balance: 1500
console.log(bankAccount.withdraw(200)); // Output: New balance: 1300

Key Points to Remember

  1. Properties describe the state or attributes of an object.

  2. Methods define the functionality or actions related to the object.

  3. Use this in methods to refer to the object itself.

So, in the end, highlighting their key components, Properties store the state or attributes of an object, while methods define the actions an object can perform.

Through clear examples, it explains basic object properties, dynamic keys, nested properties, and the use of this in methods. Advanced techniques like method chaining and combining properties with methods are also covered, demonstrating practical applications such as managing a bank account.

JavaScript

Part 6 of 10

With JavaScript, you can make your website come alive and dance like it's at a rave. But beware, because like any good party, things can get out of control quickly if you're not careful, But relax😀.

Up next

Prefix and Postfix Operators in JavaScript

Understanding JavaScript basics..

More from this blog

C

CodeWords

35 posts

Codewords is a comprehensive coding blog offering in-depth tutorials, practical tips, expert insights, and career guidance. Join us and start coding like a pro today!