Course

Web Development
  • 1.1 Introduction to the Web
  • 2.1 HTML Basics
  • 2.2 Text & Content
  • 2.3 Layout & Grouping
  • 2.4 Lists & Tables
  • 2.5 Media & Embedding
  • 2.6 Forms
  • 2.7 Semantic HTML
  • 2.8 Accessibility & SEO
  • 3.1 CSS Introduction
  • 3.2 CSS Syntax
  • 3.3 CSS Selectors
  • 3.4 Typography
  • 3.5 Colors & Backgrounds
  • 3.6 Text Styling
  • 3.7 Box Model
  • 3.8 Units
  • 3.9 Display & Positioning
  • 3.10 Layout Systems
  • 3.11 Responsive Design
  • 3.12 Animations
  • 3.13 Modern CSS
  • 3.14 Performance & Accessibility
  • 3.15 Tailwind CSS Basics
  • 4.1 Introduction to JavaScript
  • 4.2 Variables & Scope
  • 4.3 Data Types
  • 4.4 Type Conversion
  • 4.5 Operators
  • 4.6 Control Flow
  • 4.7 Loops
  • 4.8 Functions
  • 4.9 Advanced Functions
  • 4.10 Objects & Arrays
  • 4.11 The this Keyword

4.10 Objects & Arrays

Updated Jul 23, 2026

4.10 Objects & Arrays

Updated Jul 23, 2026

4.10 Objects & Arrays

Objects and Arrays are the two most commonly used data structures in JavaScript.

They help us store, organize, and manage data efficiently.

Example

const user = {

    name: "Samir",

    age: 21

};

const fruits = [

    "Apple",

    "Mango",

    "Banana"

];

Objects store related information, while arrays store ordered collections of values.


Objects

An Object is a collection of related data stored as key-value pairs.

Why Use Objects?

Instead of creating multiple variables:

const name = "Samir";
const age = 21;
const city = "Jhapa";

We can group them together.

const user = {

    name: "Samir",

    age: 21,

    city: "Jhapa"

};

Objects make related data easier to manage.


Object Syntax

const objectName = {

    key: value,

    key2: value2

};

Example

const user = {

    name: "Samir",

    age: 21,

    isStudent: true

};

Accessing Object Properties

JavaScript provides two ways to access object properties.


Dot Notation

console.log(user.name);

Output

Samir

Bracket Notation

console.log(
    user["age"]
);

Output

21

Bracket notation is useful when the property name is stored in a variable.

const key = "name";

console.log(user[key]);

Output

Samir

Updating Properties

Object properties can be changed.

user.age = 22;

console.log(user.age);

Output

22

Adding Properties

New properties can be added anytime.

user.country = "Nepal";

Result

{

    name: "Samir",

    age: 22,

    country: "Nepal"

}

Deleting Properties

Properties can also be removed.

delete user.age;

Object Methods

Objects can contain functions.

These functions are called methods.

const user = {

    name: "Samir",

    greet() {

        console.log("Hello");

    }

};

user.greet();

Output

Hello

Nested Objects

Objects can contain other objects.

const user = {

    name: "Samir",

    address: {

        city: "Jhapa",

        country: "Nepal"

    }

};

Access nested properties

console.log(
    user.address.city
);

Output

Jhapa

Arrays

An Array stores multiple values inside a single variable.


Why Use Arrays?

Instead of writing:

const fruit1 = "Apple";
const fruit2 = "Mango";
const fruit3 = "Banana";

We use:

const fruits = [

    "Apple",

    "Mango",

    "Banana"

];

Arrays are ideal for storing lists of data.


Array Syntax

const arrayName = [

    value1,

    value2,

    value3

];

Example

const colors = [

    "Red",

    "Blue",

    "Green"

];

Accessing Elements

Arrays use indexes.

0    1      2

Apple Mango Banana

Example

const fruits = [

    "Apple",

    "Mango",

    "Banana"

];

console.log(
    fruits[0]
);

Output

Apple

Array Length

console.log(
    fruits.length
);

Output

3

Adding Elements

push()

Adds an item to the end of an array.

fruits.push("Orange");

Result

[
    "Apple",
    "Mango",
    "Banana",
    "Orange"
]

Removing Elements

pop()

Removes the last element.

fruits.pop();

Beginning of an Array

unshift()

Adds an element to the beginning.

fruits.unshift("Orange");

shift()

Removes the first element.

fruits.shift();

Looping Through Arrays

The recommended way is using for...of.

for (const fruit of fruits) {

    console.log(fruit);

}

Output

Apple
Mango
Banana

Common Array Methods

Modern JavaScript provides many useful array methods.


map()

Creates a new array by transforming each element.

const numbers = [

    1,

    2,

    3

];

const doubled =
numbers.map(
    num => num * 2
);

console.log(doubled);

Output

[2, 4, 6]

filter()

Returns only elements that match a condition.

const numbers = [

    1,

    2,

    3,

    4,

    5

];

const even =
numbers.filter(
    num => num % 2 === 0
);

console.log(even);

Output

[2, 4]

find()

Returns the first matching element.

const numbers = [

    1,

    2,

    3,

    4,

    5

];

const result =
numbers.find(
    num => num > 3
);

console.log(result);

Output

4

forEach()

Executes a function for every array element.

const fruits = [

    "Apple",

    "Mango",

    "Banana"

];

fruits.forEach(fruit => {

    console.log(fruit);

});

Output

Apple
Mango
Banana

Unlike map(), forEach() does not create a new array.


JSON

JSON stands for:

JavaScript Object Notation

JSON is the standard format for exchanging data between:

  • Frontend
  • Backend
  • APIs
  • Databases

JSON Example

{
    "name": "Samir",
    "age": 21
}

Object vs JSON

JavaScript Object

const user = {

    name: "Samir"

};

JSON

{
    "name": "Samir"
}

Differences

Object JSON
Keys may omit quotes Keys require double quotes
Can contain functions Cannot contain functions
JavaScript only Language independent

JSON.stringify()

Converts an object into JSON.

const user = {

    name: "Samir"

};

const json =
JSON.stringify(user);

console.log(json);

Output

{"name":"Samir"}

JSON.parse()

Converts JSON into a JavaScript object.

const json =
'{"name":"Samir"}';

const user =
JSON.parse(json);

console.log(user);

Output

{

    name: "Samir"

}

Map

A Map stores key-value pairs similar to an object.

Unlike objects, a Map allows any data type as a key.


Creating a Map

const users =
new Map();

Adding Values

users.set(
    "name",
    "Samir"
);

Getting Values

users.get("name");

Output

Samir

Example

const map =
new Map();

map.set("name", "Samir");
map.set("age", 21);

Common Map Methods

Method Purpose
set() Add a value
get() Retrieve a value
has() Check if key exists
delete() Remove a key
clear() Remove all values
size Number of entries

Set

A Set stores only unique values.

Duplicate values are automatically removed.


Example

const numbers =
new Set([1, 2, 2, 3]);

console.log(numbers);

Result

Set(3) {1, 2, 3}

Adding Values

numbers.add(4);

Checking Values

numbers.has(2);

Output

true

Common Set Methods

Method Purpose
add() Add value
delete() Remove value
has() Check value
clear() Remove all values
size Total values

WeakMap

A WeakMap is similar to a Map but only allows objects as keys.

const weakMap =
new WeakMap();

const user = {};

weakMap.set(
    user,
    "Samir"
);

Characteristics

  • Object keys only
  • Garbage collection friendly
  • Not iterable

Why Use WeakMap?

When an object is removed from memory, its WeakMap entry is automatically removed.

This helps prevent memory leaks.


WeakSet

A WeakSet is similar to a Set but stores only objects.

const weakSet =
new WeakSet();

const user = {};

weakSet.add(user);

Characteristics

  • Objects only
  • Garbage collection friendly
  • Not iterable

Map vs Object

Object Map
String or Symbol keys Any key type
Simpler syntax More powerful
Most common Advanced use cases

Set vs Array

Array Set
Allows duplicates Unique values only
Indexed No indexes
Better for ordered lists Faster lookups and uniqueness

Real-World Example

const users = [

    {

        id: 1,

        name: "Samir"

    },

    {

        id: 2,

        name: "Ram"

    }

];

Finding a user

const user =
users.find(
    u => u.id === 1
);

console.log(user);

Output

{

    id: 1,

    name: "Samir"

}

Best Practices

Use Objects for Related Data

Good

const user = {

    name: "Samir",

    age: 21

};

Use Arrays for Lists

Good

const users = [

    "Samir",

    "Ram",

    "Hari"

];

Prefer Array Methods

Use methods like map(), filter(), and find() instead of manually writing loops whenever appropriate.

const adults =
users.filter(
    user => user.age >= 18
);

Use Map for Dynamic Keys

const cache =
new Map();

Use Set to Remove Duplicates

const uniqueNumbers =
[...new Set(numbers)];

This is a common technique in modern JavaScript.


Summary

Concept Purpose
Object Store key-value pairs
Array Store ordered values
JSON Data exchange format
Map Flexible key-value storage
Set Unique value collection
WeakMap Memory-efficient Map
WeakSet Memory-efficient Set

Key Takeaway

Modern JavaScript applications rely heavily on Objects and Arrays.

Object
   ↓
Store Related Data

Array
   ↓
Store Lists

JSON
   ↓
Transfer Data

Map
   ↓
Flexible Key-Value Storage

Set
   ↓
Unique Values

WeakMap / WeakSet
        ↓
Memory Optimization

A typical frontend application's data flow looks like this:

API Response
      ↓
     JSON
      ↓
JavaScript Objects
      ↓
Arrays
      ↓
UI Rendering

Understanding Objects and Arrays is essential because React state, API responses, database records, forms, and most application logic are built around these data structures.

4.9 Advanced Functions4.11 The this Keyword