JavaScript collections refer to data structures in the JavaScript programming language that are used to store and manipulate collections of values or objects. These collections provide various ways to organize and access data efficiently. There are several built-in collection types in JavaScript, and developers can also create custom collections as needed.
Arrays are ordered collections of values, and they are perhaps the most commonly used data structure in JavaScript. Elements in an array can be accessed by their index, and arrays can hold values of different data types.
let myArray = [1, 2, 3, 4, 5];
console.log(myArray[0]); // Accessing the first element
Objects in JavaScript are collections of key-value pairs. They are versatile and can be used to represent a wide range of data structures. Objects are often used for creating dictionaries, maps, and records.
let person = {
name: "Maria",
age: 28,
city: "New York"
};
console.log(person.name); // Accessing a property
Keys are always strings (or Symbols, introduced in ES6). When you use non-string values as keys in an object, JavaScript implicitly converts them to strings.
Objects are generally used for a simple dictionary-like structure with string keys.