0% found this document useful (0 votes)
6 views3 pages

Understanding JavaScript Data Structures

The document provides an overview of data structures in programming, focusing on arrays, objects, sets, and maps. It explains how to create, manipulate, and access elements in these data structures, along with their respective methods. Key examples and descriptions illustrate the functionality of each structure, highlighting their unique characteristics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Understanding JavaScript Data Structures

The document provides an overview of data structures in programming, focusing on arrays, objects, sets, and maps. It explains how to create, manipulate, and access elements in these data structures, along with their respective methods. Key examples and descriptions illustrate the functionality of each structure, highlighting their unique characteristics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Data Structures

---------------
Array
-----
An array is a special variable that can hold multiple values in a single name.
let fruits = ["Apple", "Banana", "Mango"];
[Link](fruits); // ["Apple", "Banana", "Mango"]
[Link](fruits[1]); // Banana

Add element
[Link]("Orange"); // Adds at the end
[Link]("Kiwi"); // Adds at the beginning
[Link](fruits); // ["Kiwi", "Apple", "Banana", "Mango", "Orange"]

Remove Element
[Link](); // Removes last element
[Link](); // Removes first element
[Link](fruits); // ["Apple", "Banana", "Mango"]

Array Methods
--------------

| Method | Description | Example |


| ------------ | ------------------------ | ------------------------------ |
| `push()` | Adds element at end | `[Link]("Grapes")` |
| `pop()` | Removes last element | `[Link]()` |
| `shift()` | Removes first element | `[Link]()` |
| `unshift()` | Adds element at start | `[Link]("Kiwi")` |
| `concat()` | Combines arrays | `[Link](arr2)` |
| `slice()` | Returns part of an array | `[Link](1, 3)` |
| `splice()` | Add/Remove elements | `[Link](2, 1, "Guava")` |
| `indexOf()` | Finds index of element | `[Link]("Banana")` |
| `includes()` | Checks if element exists | `[Link]("Apple")` |
| `sort()` | Sorts array | `[Link]()` |
| `reverse()` | Reverses array | `[Link]()` |

Object
---------
An object is a collection of key–value pairs.
Each key (also called a property) has a value — which can be a string, number,
array, another object, or even a function
let person = {
name: "John",
age: 25,
city: "New York",
greet: function() {
[Link]("Hello, my name is " + [Link]);
}
};

Accessing Object Properties


---
[Link]([Link]); // John
[Link](person["age"]); // 25

➕ Add a new property


[Link] = "USA";

Update property
[Link] = 30;

delete property
delete [Link];

Set
====

A Set is a collection of unique values — meaning it automatically removes


duplicates.

You can store any type of value (numbers, strings, objects, etc.) in a Set.

let mySet = new Set([10, 20, 30, 10]);


[Link](mySet); // Set(3) {10, 20, 30}

Set Methods
------------
| Method | Description | Example |
| --------------- | -------------------------------- | ------------------------ |
| `add(value)` | Adds a new element | `[Link](40)` |
| `delete(value)` | Removes a specific element | `[Link](20)` |
| `has(value)` | Checks if a value exists | `[Link](30)` → `true` |
| `clear()` | Removes all elements | `[Link]()` |
| `size` | Returns total number of elements | `[Link]` → `3` |

Example
---------
let fruits = new Set();

[Link]("Apple");
[Link]("Banana");
[Link]("Mango");
[Link]("Apple"); // duplicate ignored

[Link](fruits); // Set(3) {"Apple", "Banana", "Mango"}


[Link]([Link]); // 3
[Link]([Link]("Banana")); // true

[Link]("Mango");
[Link](fruits); // Set(2) {"Apple", "Banana"}

Map
======
A Map is a collection of key–value pairs, just like an object —
but it offers better performance, order preservation, and allows any data type as a
key (not just strings or symbols).

let myMap = new Map();

[Link]("name", "Alice");
[Link]("age", 25);
[Link]("city", "London");

[Link](myMap);
// Map(3) {"name" => "Alice", "age" => 25, "city" => "London"}

set Methods
-----
| Method / Property | Description | Example
|
| ----------------- | ----------------------------------- |
------------------------------- |
| `set(key, value)` | Adds or updates a key-value pair | `[Link]("country",
"UK")` |
| `get(key)` | Returns the value for the given key | `[Link]("name")` →
`"Alice"` |
| `has(key)` | Checks if key exists | `[Link]("age")` →
`true` |
| `delete(key)` | Removes a key-value pair | `[Link]("city")`
|
| `clear()` | Removes all entries | `[Link]()`
|
| `size` | Returns total number of entries | `[Link]` → `3`
|

Example
-------
let user = new Map();

[Link]("id", 101);
[Link]("name", "John");
[Link]("role", "Tester");

[Link]([Link]("name")); // John
[Link]([Link]("role")); // true
[Link]([Link]); // 3

[Link]("role");
[Link]([Link]); // 2

You might also like