Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Object Methods in JavaScript

User image

Published by

sanya sanya

Published at: 6th May, 2023
1.59 mins read

Objects Methods in Javascript are the actions that can be performed on the objects. It is considered as the property that defines the properties of the objects.

This can be understood with the help of a simple example, In the image mentioned below, the man has several properties such as he is brother, son, and husband. Later on, he got a child, and a new property of his father is also added to him.


How to Access Object Methods

The syntax for accessing the Object Methods is mentioned below -

objectName.methodName()

The Object Methods can be accessed by following the points mentioned below -

  1. For instance, if the object name is Object(), then the property will be called an "Object".
  2. The "Object" Property will be executed when called with ()
  3. If we try to "Object" Property without "()", the return value will be `function definition`.

How to Add Method to an Object

The code by which we can add a Method to an Object is mentioned below -

let person = { name: "Paul", age: 10, gender: "Male" }; person.getInfo = function() { return this.name + this.age + this.gender; }; console.log(person.getInfo());
//Output: Paul 10 Male

In this example, we define the "getInfo" method for the "person" object by using the dot notation to add the method to the object. Further, We define the method as a function that uses the "this" keyword to refer to the current instance of the "person" object.

The method uses the properties of the object to construct and return a string with the person's information.

There are build-in functions available which we can add after the return statement accordingly. for instance,

person.getInfo = function() { return ("Name: " + this.name + ", Age: " + this.age + ", Gender: " + this.gender).toLowerCase(); };

This will provide the output in the Lower Case.

Library

WEB DEVELOPMENT

Basic

HTML - Hyper Text Markup Language

CSS - Cascading Style Sheets

JavaScript

An Introduction to Javascript!

How to Run JavaScript Code

Variables in Javascript

Numbers in JavaScript

JavaScript Operators

Data Types in JavaScript

Conditional Statements

Switch Statements

Loops in Javascript

Arrays in JavaScript

Strings in JavaScript

Objects in JavaScript

Object Methods in JavaScript

Functions in JavaScript

Object Referencing and Copying in JavaScript

' this' keyword

Asynchronous Programming in JavaScript

Callbacks in JavaScript

Promises in JavaScript

Constructor Functions in JavaScript

Async and Await in JavaScript

Type Conversion in Javascript

DOM

Currying in JavaScript

Network Request

Frontend

Backend

Interview Questions

FAANG QUESTIONS

On this page

How to Access Object Methods

How to Add Method to an Object