- Published on
Basic Data Structures in JavaScript
- Authors
- Name
- Phil Cool Beans
- @PhilCoolGames
JavaScript has several built-in data structures that can be used to store and manipulate data
Arrays: Arrays are used to store multiple values in a single variable. They can be created using square brackets and can hold any type of data, including strings, numbers, and other arrays.
Example:
javascript
const myArray = [1, 2, 3, "four", "five"];
Objects: Objects are used to store key-value pairs. They can be created using curly braces and can hold any type of data.
Example:
javascript
const myObject = ;
Maps: Maps are used to store key-value pairs where the key can be any type of data, not just strings. Maps provide methods for adding, retrieving, and deleting key-value pairs.
Example:
javascript
const myMap = new Map(); myMap.set("name", "John"); myMap.set(1, "one");
Sets: Sets are used to store unique values of any type. They provide methods for adding, retrieving, and deleting values.
Example:
javascript
const mySet = new Set(); mySet.add("apple"); mySet.add("orange"); mySet.add("apple"); // duplicates are ignored
Strings: Strings are used to represent text. They can be created using single or double quotes.
Example:
javascript
const myString = "Hello, world!";
Numbers: Numbers are used to represent numerical values, including integers and floating-point numbers.
Example:
javascript
const myNumber = 42;
These are just a few of the basic data structures available in JavaScript. There are many more advanced data structures available in libraries and frameworks, such as trees, graphs, and stacks.