Skip to main content

Command Palette

Search for a command to run...

JavaScript Object and it’s method

Updated
•2 min read•View as Markdown
JavaScript Object and it’s method
Z

I am writing to express my interest in being front end developer. As a student and a constant learner. I have learned Html, CSS, JS, React.js, Tailwind , bootstrap and I aim to attain an engaging Internship for the same. I am willing to give my time and get an exposure and hands on as well as test my understanding on the technologies.

What is Object in JavaScript?

In JavaScript, objects are king. If you understand objects, you understand JavaScript.

In JavaScript, almost "everything" is an object.

  • Booleans can be objects (if defined with the new keyword)

  • Numbers can be objects (if defined with the new keyword)

  • Strings can be objects (if defined with the new keyword)

  • Dates are always objects

  • Maths are always objects

  • Regular expressions are always objects

  • Arrays are always objects

  • Functions are always objects

  • Objects are always objects

All JavaScript values, except primitives, are objects

Syntax

const person = {
        firstName:"John",
        lastName:"Doe", 
        age:50, 
        eyeColor:"blue"
};

JavaScript Object Methods

JavaScript methods are actions that can be performed on objects.

A JavaScript method is a property containing a function definition.

PropertyValue
firstNameJohn
lastNameDoe
age50
eyeColorblue

Accessing Object Methods

You access an object method with the following syntax:

objectName.methodName()

Example :

name = person.fullName();

Adding a Method to an Object

person.name = function () {
  return this.firstName + " " + this.lastName;
};

Using Built-In Methods

This example uses the toUpperCase() method of the String object, to convert a text to uppercase:

let message = "Hello world!";
let x = message.toUpperCase();
//Output :  HELLO WORLD!

Resources :

w3school