Unknown model· unknown
from ShareGPT 90K (shared ChatGPT conversations)
tell me about type of datastructures in javascript with examples, also highlight the…
- User
tell me about type of datastructures in javascript with examples, also highlight the most widely used ones
- Assistant
There are several types of data structures in JavaScript, including:
- Array - a collection of items stored at contiguous memory locations. Example:
codelet fruits = ["apple", "banana", "orange"];- Object - a collection of key-value pairs. Example:
codelet person = { name: "John", age: 30, city: "New York" };- Map - a collection of key-value pairs with built-in methods for adding, removing, and searching for items. Example:
codelet map = new Map(); map.set("name", "John"); map.set("age", 30); console.log(map.get("name")); // John- Set - a collection of unique items. Example:
codelet set = new Set(); set.add(1); set.add(2); console.log(set.size); // 2- String - a sequence of characters. Example:
codelet name = "John";- Linked List - a linear collection of data elements, called nodes, pointing to the next node by means of a pointer. Example:
codeclass LinkedList { constructor() { this.head = null; this.size = 0; } add(element) { var node = new Node(element); var current; if (this.head == null) this.head = node; else { current = this.head; while (current.next) { current = current.next; } current.next = node; } this.size++; } }The most widely used data structures in JavaScript are arrays, objects, and strings.