-
Definition: An array is a data structure in JavaScript used to store multiple values in a single variable.
let fruits = ["apple", "banana", "cherry"];
-
Index: Each item in an array is assigned a number starting from 0, which is called its index.
fruits[0]; // "apple"
-
Length: You can find out how many items are in an array with
.length
.fruits.length; // 3
-
Array Methods:
-
.push()
: Add an element to the end. -
.pop()
: Remove the last element. -
.join()
: Combine all elements into a string. -
.splice()
: Add or remove elements from the array.fruits.push("grape"); // Adds "grape" to the end fruits.pop(); // Removes the last item ("grape")
-
-
Definition: Objects are collections of key-value pairs. They help associate different values together.
let book = { title: "Harry Potter", author: "J.K. Rowling", published: 1997 };
-
Properties: The keys in an object (like
title
,author
, etc. in the above example) are called properties. -
Accessing Values: Use dot notation to access values in an object.
book.title; // "Harry Potter"
-
Arrays in Objects: Objects can hold arrays as values.
let student = { name: "Alice", subjects: ["Math", "Science", "English"] }; student.subjects[1]; // "Science"
- Arrays are ordered lists, and you can access any element by its index.
- Objects store data as key-value pairs and are great for representing entities with various attributes.
- Always use the right data structure for your needs; sometimes, you might even use arrays and objects together!
-
Always comment your code! It helps in understanding what each part of the code does.
// This is a single line comment in JavaScript
-
If you're unsure about a method or property, search online or revisit this README.
Happy coding!