If arrays are ordered lists, Objects are structured dictionaries. Objects in JavaScript allow you to group related state and behavior into a single data structure composed of key-value pairs (also called properties and methods).
The simplest and most common way to create an object is using curly braces { ... }:
1 const student = {
2 // Properties (key: value)
3 firstName : "Sumit" ,
4 lastName : "Sharma" ,
5 age : 24 ,
6 isEnrolled : true ,
7 skills : [ "HTML" , "CSS" , "JavaScript" ] ,
8 address : {
9 city : "New Delhi" ,
10 country : "India"
11 } ,
12
13 // Method (function inside an object)
14 getFullName : function ( ) {
15 return ` ${ this . firstName } ${ this . lastName } ` ;
16 }
17 } ;
2. Accessing and Modifying Properties JavaScript offers two ways to access object properties:
A. Dot Notation (object.property) Clean, standard, and easy to read:
1 console . log ( student . firstName ) ; // "Sumit"
2 console . log ( student . address . city ) ; // "New Delhi"
3
4 // Calling a method:
5 console . log ( student . getFullName ( ) ) ; // "Sumit Sharma"
B. Bracket Notation (object["property"]) Mandatory in two critical scenarios:
1 When property names contain spaces or hyphens :
1 const config = { "api-version" : "v2" , "max retries" : 3 } ;
2 console . log ( config [ "api-version" ] ) ; // Valid! config.api-version would fail!
1 When the property key is stored in a dynamic variable :
1 const keyToLookUp = "age" ;
2 console . log ( student [ keyToLookUp ] ) ; // 24!
3 // student.keyToLookUp would look literally for a property named 'keyToLookUp'!
3. Adding, Updating, and Deleting Properties Objects in JavaScript are dynamic:
1 const course = {
2 title : "Full-Stack Development" ,
3 duration : "6 months"
4 } ;
5
6 // 1. Updating an existing property:
7 course . duration = "12 months" ;
8
9 // 2. Adding a brand new property:
10 course . instructor = "Sumit Sir" ;
11 course . rating = 4.9 ;
12
13 // 3. Deleting a property using the 'delete' keyword:
14 delete course . rating ;
15
16 console . log ( course ) ;
17 // { title: "Full-Stack Development", duration: "12 months", instructor: "Sumit Sir" }
1 const user = { name : "Aarav" , email : "aarav@gmail.com" } ;
2
3 // 1. The 'in' operator:
4 console . log ( "email" in user ) ; // true
5 console . log ( "phoneNumber" in user ) ; // false
6
7 // 2. Object.hasOwn() (Modern ES2022 standard):
8 console . log ( Object . hasOwn ( user , "name" ) ) ; // true
When a function is called as a method of an object, this refers to the object itself:
1 const car = {
2 brand : "Tesla" ,
3 speed : 60 ,
4 accelerate ( ) {
5 this . speed += 20 ;
6 console . log ( ` ${ this . brand } is now moving at ${ this . speed } km/h. ` ) ;
7 }
8 } ;
9
10 car . accelerate ( ) ; // "Tesla is now moving at 80 km/h."
Interactive Quiz • Knowledge Check
Practice Quiz What data structure does a JavaScript object use to store data?
A A continuous sequence of binary bits
B Key-value pairs (properties and methods)
C LIFO stack frames
D CSV columns
When MUST you use bracket notation (obj[key]) instead of dot notation (obj.key)?
A Whenever the object contains numbers
B When property names have spaces/hyphens or when the property name is held inside a dynamic variable
C Only inside loops
D Only on Tuesdays
What keyword is used to remove a property from an object in JavaScript?
A remove
B delete
C discard
D pop
Inside an object method, what does the this keyword refer to when invoked normally?
A The global window object
B The object on which the method was called
C The browser DOM
D Undefined
What is the modern ES2022 method to safely check if an object owns a specific property key?
A Object.hasOwn(obj, "key")
B obj.contains("key")
C obj.hasKey("key")
D Object.exists(obj, "key")