JavaScript Tutorial

Javascript Objects

In JavaScript, objects are a fundamental data structure used to store and organize related data and functionality. Objects are created using curly braces ({}) and consist of key-value pairs, where each key is a string (also known as a property name) and each value can be of any data type, including other objects.

JavaScript is an object-based language. Everything is an object in JavaScript.

  • JavaScript is template-based not class-based. Here, we do not create the class to get the object. But, we directly create the objects.
  • Objects in Javascript are basically data structures for storing and manipulating data. They help you structure your data and provide methods (or functions) for manipulating the data stored at any given time within your application. There are methods for creating them, adding properties, deleting properties etc.
  • JavaScript provides several object methods that can be used to manipulate that data the way you want it.

A good example usage of objects:

Let's say we are creating a class attendance app. You can categorize your data and structure them in objects like so;

Accessing Object Properties

You can access object properties using dot notation or bracket notation.

myObjName.propertyName

or

myObjName["propertyName"]

Creating Objects in JavaScript

There are 3 ways to initialize an object.

Object literal Notation

The syntax for creating an object using object literal is given below:

let person = { name: "John",  age: 30,  city: "New York"};

As we can see above, property and value are separated by : (colon).

Let’s see by a simple example of creating an object in JavaScript.

Input:-

Output:-

By creating an instance of Object directly (using new keyword)

The syntax to create an object directly is given below:

Input:-

Output:-

Object Constructor

Now, we need to create a function with arguments or parameters. Each argument value can be assigned in the current object by using “this” keyword.

The this keyword will refer to the current object.

An example of creating an object using object constructor is given below.

Input:-

Output:-

 

Defining method in JavaScript Object

In JavaScript, you can define methods in objects by assigning a function to a property of an object. The property acts as the method name, and the assigned function becomes the method's implementation. Here's an example of defining a method in an object:

Input:-

Output:-

 

JavaScript Object Methods

The different methods for an Object are given below:

S.No

Methods

Description

1

Object.assign()

Used to copy enumerable and own properties from a source object to a target object

2

Object.create()

Used to create a new object with the specified prototype object and properties.

3

Object.defineProperty()

Used to describe some behavioral attributes of the property.

4

Object.defineProperties()

Used to create or configure multiple object properties.

5

Object.entries()

Returns an array with arrays of the key, value pairs.

6

Object.freeze()

Prevents existing properties from being removed.

7

Object.getOwnPropertyDescriptor()

Returns a property descriptor for the specified property of the specified object.

8

Object.getOwnPropertyDescriptors()

Returns all own property descriptors of a given object.

9

Object.getOwnPropertyNames()

Returns an array of all properties (enumerable or not) found.

10

Object.getOwnPropertySymbols()

Returns an array of all own symbol key properties.

11

Object.getPrototypeOf()

Returns the prototype of the specified object.

12

Object.is()

This method determines whether two values are the same value.

13

Object.isExtensible()

This method determines if an object is extensible

14

Object.isFrozen()

This method determines if an object was frozen.

15

Object.isSealed()

This method determines if an object is sealed.

16

Object.keys()

Returns an array of a given object's own property names.

17

Object.preventExtensions()

Used to prevent any extensions of an object.

18

Object.seal()

Prevents new properties from being added and marks all existing properties as non-configurable.

19

Object.setPrototypeOf()

This method sets the prototype of a specified object to another object.

20

Object.values()

Returns an array of values.

Do Not Declare Strings, Numbers, and Booleans as Objects!

When a JavaScript variable is defined with the keyword "new", the variable is created as an object:

var a = new String();        // Declares a as a String object

var b = new Number();        // Declares b as a Number object

var c = new Boolean();       // Declares c as a Boolean object

Note:- Avoid String, Number, and Boolean objects. They complicate your code and slow down execution speed.

Go back to Previous Course