Understanding Objects
Learn to work with objects in JavaScript using built-in and common methods.
We'll cover the following...
Objects in JavaScript are versatile data structures that allow us to store and organize data as key-value pairs. A key is a unique identifier (usually a string or symbol), and its associated value can be any JavaScript data type, including numbers, strings, arrays, other objects, or functions.
Creating an object
The syntax for defining objects in JavaScript is given below.
const objectName = {key1: value1,key2: value2,key3: value3};
Syntax for objects in JavaScript
Let’s create a car
object to illustrate how objects work:
const car = {brand: "Toyota",model: "Corolla",year: 2022,start: function() {console.log("Car started!");}};
Creating a car object in JavaScript
In the above code:
Properties: The
brand
,model
, andyear
are key-value pairs.Method: The
start
is a function that belongs to thecar
object.
We can access the data stored in these objects in the following ways.
Dot notation:
car.brand
returns ...
Access this course and 1400+ top-rated courses and projects.