7. Arrays

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Array (code)

= ['item1', 'item2', 'item3'];

.unshift array Method

Adds one or more elements to beginning of array and returns new length. array.unshift(item1, item2, ...);

Array (definition)

JavaScript's way of making lists. Arrays can store any data types (including strings, numbers, and booleans). Like lists, arrays are ordered, meaning each item has a numbered position.

.shift() array Method

Removes the first element of the array. All subsequent elements will shift down one place. array.shift();

what happens if we try to change an array inside a function? Does the array keep the change after the function call or is it scoped to inside the function?

When you pass an array into a function, if the array is mutated inside the function, that change will be maintained outside the function as well. You might also see this concept explained as pass-by-reference since what we're actually passing to the function is a reference to where the variable memory is stored and changing the memory.

The .push() Method

adds items to the end of an array sampleVariable.push('new item 3', 'new item 4);

nested array

array containing another array const nestedArr = [[1], [2, 3]];

index

array item's numbered position We can access individual items using their index, which is similar to referencing an item in a list based on the item's position

.length array property

built-in property returns # of items in an array accessed same way as with strings: console.log(arrayVariable.length);

synatax for accessing individual characters in a string

const hello = 'Hello World'; console.log(hello[6]); // Output: W //The console will display W since it is the character that is at index 6.

zero-indexed

index positions start counting from 0 rather than 1. Therefore, the first item in an array will be at position 0

syntax to access the element using index number

let cities = ['New York', 'Beijing', 'Nairobi']; cities[0] //returns 'New York'

how to update an array's value

let seasons = ['Winter', 'Spring', 'Summer', 'Fall']; seasons[3] = 'Autumn'; console.log(seasons); //Output: ['Winter', 'Spring', 'Summer', 'Autumn']

.join() method

method to join each element of an array into a string separated by whatever delimiter you provide as an argument. ex: var veggies = ["Celery", "Radish", "Carrot", "Potato"]; var salad = veggies.join(" and "); console.log(salad); // "Celery and Radish and Carrot and Potato"

can you reassign a new array or a different value to a const array?

no

pop method

removes the last item of an array does not take any arguments = sampleArray.pop( );

.slice( ) array Method

returns a shallow copy of all or part of an array without modifying the original. A shallow copy duplicates the contents of an object into a new instance by reference; which is why changes to the copy are not reflected in the original. The returned array contains the element specified by the first argument and all subsequent elements up to, but not including, the element specified by the second argument. array.slice(start, end); start: The start index of the slice to be returned (optional) end: The end index of the slice to be returned (optional) -- If only one argument is specified, the returned array contains all elements from the start position to the end of the array. array.slice(start); -- If start and end values are not provided, the method will return a copy of the whole array. array.slice(); -- A negative index can be used, indicating an offset from the end of the sequence. --- one argument uses the index from that argument to the end. -- zero arguments outputs the entire array.

.indexOf( ) Array Method

returns the first index at which an element can be found. Returns -1 if the element is not found. array.indexOf(element, startIndex); The following parameters are used in the .indexOf() method: -The element to be searched for in the array. -The optional startIndex position to begin searching from. If one is not given, the search starts from the beginning of the array. Negative indices will offset from the end of the array.

array literal

syntax that defines an array using a comma-separated list of values within a set of square brackets [ ] Each content item inside an array is called an element. Each element inside the array is a different data type.

what happens if you try to access an index that is beyond the last element?

undefined

how to access nested arrays

use bracket notation with the index value, just like we did to access any other element: const nestedArr = [[1], [2, 3]]; console.log(nestedArr[1]); // Output: [2, 3] if we wanted to access the elements within the nested array we can chain, or add on, more bracket notation with index values. const nestedArr = [[1], [2, 3]]; console.log(nestedArr[1]); // Output: [2, 3] console.log(nestedArr[1][0]); // Output: 2

Can an array be saved to a variable?

yes

Can you change the contents of a const array?

yes

can you reassign a new array or a different value to a let array?

yes

is JS zero-indexed?

yes


Ensembles d'études connexes