-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharray_functions_script.js
43 lines (31 loc) · 1.58 KB
/
array_functions_script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
let pens = ["red", "blue", "green", "orange"];
console.log("Before: ", pens);
// PROPERTIES:
// Get a property of an object by name:
// console.log("Array length: ", pens.length);
// METHODS:
// Reverse the array:
// pens.reverse();
// Remove the first value of the array:
// pens.shift();
// Add comma-separated list of values to the front of the array:
// pens.unshift("purple", "black");
// Remove the last value of the array:
// pens.pop();
// Add comma-separated list of values to the end of the array:
// pens.push("pink", "prussian blue");
// Find the specified position (pos) and remove n number of items from the array. Arguments: pens.splice(pos,n):
// pens.splice(1, 2) // Starts at the seccond item and removes two items.
// console.log("After: ", pens);
// Create a copy of an array. Typically assigned to a new variable:
// const newPens = pens.slice();
// console.log("New pens: ", newPens);
// Return the first element that matches the search parameter after the specified index position.
// Defaults to index position 0. Arguments: pens.indexOf(search, index):
// const result = pens.indexOf("green", 0);
// console.log("The search result index is: ", result);
// Return the items in an array as a comma separated string. The separator argument can be used to change the comma to something else. Arguments: pens.join(separator):
// const arrayString = pens.join(',');
// console.log("String from array: ", arrayString);
// MDN documentation for Array:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array