JavaScript Object Methods

Object methods are functions stored as properties within JavaScript objects. They enable objects to perform actions and define their behavior.

These methods can be either custom-defined or built-in functions provided by JavaScript, allowing you to manipulate objects or retrieve information from them efficiently.



Understanding Object Methods in JavaScript

In JavaScript, you can store functions inside objects. These functions are called methods. Methods allow objects to perform actions or behaviors.

Just like other properties, methods are written using key-value pairs — but the value is a function.

Here's the basic syntax for creating a method inside an object:

javascript
const objectName = {
  methodName: function() {
    // code to run
  }
};
  • objectName: The name of your object, like user or car.
  • methodName: The name you give the method (usually a verb like start, greet, or calculate).
  • function: A block of code that performs an action when the method is called.
  • this: A special keyword used inside methods to refer to the object itself.

Let’s see a simple example where we define a method inside an object and then call it:

javascript
const user = {
  name: "Tom",
  greet: function() {
    console.log("Hello, " + this.name + "!");
  }
};

user.greet();

What’s Happening Here:

  • name: This is a property that stores the user's name.
  • greet: function() { ... }: This defines a method called greet.
  • this.name: Inside the method, this refers to the object itself, so this.name gives "Tom".
  • user.greet(): This calls the method and runs the code inside it.

Output:

Hello, Tom!

Methods are powerful because they allow your objects to “do things” — not just store data. You’ll see them used everywhere in JavaScript programming.


Built-in Object Methods

JavaScript provides several built-in methods that make working with objects easier. These methods help you retrieve keys, values, or entries from an object.

These methods are part of the global Object class, and they work on plain JavaScript objects.

MethodDescription
Object.keys()Returns an array of all keys in the object.
Object.values()Returns an array of all values in the object.
Object.entries()Returns an array of key-value pairs as arrays.
Object.assign()Copies properties from one or more objects into another object.
Object.freeze()Makes an object read-only by preventing changes to its properties.
Object.seal()Prevents adding or removing properties, but allows value updates.
Object.hasOwn()Checks if an object has a specific property of its own (not inherited).
Object.fromEntries()Creates a new object from an array of key-value pairs.

Here's an example that uses methods:

Example:

javascript
// Common Object Methods in JavaScript

const user = {
  name: "Tom",
  age: 25
};

// 1. Object.keys()
console.log("Keys:", Object.keys(user));

// 2. Object.values()
console.log("Values:", Object.values(user));

// 3. Object.entries()
console.log("Entries:", Object.entries(user));

// 4. Object.hasOwn()
console.log("Has 'name':", Object.hasOwn(user, "name"));
console.log("Has 'email':", Object.hasOwn(user, "email"));

// 5. Object.freeze()
Object.freeze(user);
user.age = 30; // Will not change
console.log("After freeze (age):", user.age);

// 6. Object.seal()
const sealedUser = { country: "USA" };
Object.seal(sealedUser);
sealedUser.country = "Canada";  // Allowed
sealedUser.city = "Toronto";   // Not added
console.log("Sealed user:", sealedUser);

// 7. Object.assign()
const extra = { email: "Tom@example.com" };
const fullUser = Object.assign({}, user, extra);
console.log("Merged user:", fullUser);

// 8. Object.fromEntries()
const entries = [["x", 10], ["y", 20]];
const objFromEntries = Object.fromEntries(entries);
console.log("Object from entries:", objFromEntries);

Output

Keys: [ 'name', 'age' ]
Values: [ 'Tom', 25 ]
Entries: [ [ 'name', 'Tom' ], [ 'age', 25 ] ]
Has 'name': true
Has 'email': false
After freeze (age): 25
Sealed user: { country: 'Canada' }
Merged user: { name: 'Tom', age: 25, email: 'Tom@example.com' }
Object from entries: { x: 10, y: 20 }

Example 1: Creating an Object Method

Let’s create a method that prints a greeting using the object's property.

javascript
const person = {
  name: "charlie",
  greet: function() {
    console.log("Hi, my name is " + this.name);
  }
};

person.greet();

What’s Happening Here:

  • name: A property storing the person’s name.
  • greet: A method defined as a function inside the object.
  • this.name: Refers to the name property of the same object.
  • person.greet(): Calls the method, executing the function inside.

Using methods, you can make your objects act like mini-programs that know how to work with their own data.

Output:

Hi, my name is charlie

Example 2: Creating an Object Method Using Shorthand

JavaScript allows you to define methods more concisely using **method shorthand** introduced in ES6. This is functionally identical to the regular method syntax but looks cleaner.

javascript
const dog = {
  name: "Tofu",
  bark() {
    console.log(this.name + " says woof!");
  }
};

dog.bark();

What’s Happening Here:

  • bark(): This is shorthand for bark: function() { ... }.
  • this.name: Refers to the name property of the object.
  • dog.bark(): Calls the method, outputting the message to the console.

The shorthand syntax is cleaner and commonly used in modern JavaScript. It’s especially useful when defining multiple methods.

Output:

Tofu says woof!

Exercises

1. Create an object with a method that says goodbye.
javascript
const person = {
  name: "Jane",
  sayGoodbye: function() {
    console.log("Goodbye, " + this.name + "!");
  }
};

person.sayGoodbye();

2. Use Object.keys() on an object with 3 properties.
javascript
const profile = { name: "Jane", age: 28, city: "Paris" };
console.log(Object.keys(profile));

3. Use Object.assign() to merge two objects.
javascript
const a = { x: 1 };
const b = { y: 2 };
const c = Object.assign({}, a, b);
console.log(c); // { x: 1, y: 2 }

Frequently Asked Questions

What are object methods in JavaScript?

Object methods are functions stored as properties within an object. They let the object perform specific actions or behaviors.


How do you define a method inside a JavaScript object?

You define a method by assigning a function to a property. For example: methodName: function() or using the shorthand: methodName() .


How do you call an object method?

Use dot notation followed by parentheses, like object.methodName(), to execute the method.


Can object methods access other properties within the same object?

Yes! Inside a method, you can use this.propertyName to refer to other properties of the same object.


What is the shorthand syntax for defining methods in objects?

Instead of writing methodName: function() , you can write methodName() . This shorthand is cleaner and more concise.



What’s Next?

Next, you’ll learn about the this keyword, which lets object methods access and manipulate their own properties.