DEV Community

Dillon
Dillon

Posted on

Understanding Arrays

To create an array

const = arrayName = [];

To add items to an array, you can use the .push

myToDoList.push(todoA, todoB);

alternatively you could do
myToDoList.push(todoA);
myToDoList.push(todoB);
myToDoList.push(todoc);

to return the last item

console.log(myToDoList.[myTodoList.length-1])

to remove the last item

myToDoList.pop();

Top comments (2)

Collapse
 
dillpap profile image
Dillon

You can destructure arrays!!!

It's very similar to object destructuring

for example,

const books = [
"My Anthonia",
"The Picture of Dorian Grey",
"A Scarlet Letter"
];

To destructure this array...

const [first, second, third]] = books

If you wanted to destructure books to show only My Anthonia, and create an array with the other two.

you can use an operator that is like the spread operator.

const [favBook, ...otherFavs] = books;

Collapse
 
dillpap profile image
Dillon

.indexOf tells us where the item is in the array. If not there, it returns a -1

.includes returns a boolean.

syntax => arrayName.includes(itemYouAreTesting)

using the callback function the full syntax is

arrayName.some(arrayName => arrayName.value === true);

basically same syntax as before, but the .every method checks if EVERY unit meets the criteria