-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript-arrays.js
99 lines (64 loc) · 2.42 KB
/
javascript-arrays.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// // Javascript Arrays Notes
// // definition: data structure that is used to store a collection of data in an indexed list
// // [1, 2, 3, 4, 5]
// // -each item in an array is called an element
// // -elements are separated by commas
// // -surrounded by square brackets
// // -zero-based index
// // .length - returns the number of elements in the array
// // [1, 2, 3, 4, 5]
// // first index in array is always 0
// // indexes 0, 1, 2, 3, 4
// let numbers = [1, 2, 3, 4, 5]
// console.log(numbers)
// // BRACKET NOTATION
// // allows us to access elements by index
// // syntax - brackets with desired index inside, example: [3]
// console.log(numbers[3])
// console.log(numbers.length)
// console.log(numbers[6])
// console.log(numbers[6]= 49)
// console.log(numbers)
// console.log(numbers[5])
// let newArray = ["cat", "dog", true, 42, false]
// // let empty = [] - you can assign an empty array.
// // ----------- BUILT IN METHODS -----------
// // SETTERS - mutators - they change the original array
// // GETTERS - accessors - get the element so you can use it, but doesn't change the array
// // SETTERS
// // .push() adds an item to the END of the array
// console.log(numbers)
// console.log(numbers.push(7)) // returns the length of the array
// console.log(numbers)
// // .unshift() adds to the BEGINNING OF ARRAY
// console.log('start ', numbers)
// numbers.unshift(74)
// console.log('end ', numbers)
// // .pop() REMOVES LAST ELEMENT from the array and it returns its VALUE
// console.log(numbers.pop())
// console.log(numbers)
// // .shift() REMOVES THE FIRST ELEMENT from the array and it returns its VALUE
// console.log(numbers.shift())
// console.log(numbers)
// let removedItem = numbers.shift()
// console.log(removedItem)
// GETTERS
// .concat() add multiple arrays
// calling the method on one array and then passing the other array as an argument
//
let nums = [1, 2, 3]
let bigNums = [36, 42, 97]
let strings = ["Tawn", "Michy", "Eric"]
console.log(nums.concat(bigNums))
console.log(nums.concat(strings))
console.log(nums.concat(bigNums).concat(strings))
// .split()
let splitString = "1034".split("")
console.log(splitString)
// .join()
// console.log(split.join(""))
// DESTRUCTURING
// assignment syntax that makes it possible to unpack values from array into distinct variables
let fullName = ["Tricia", "Sykes"]
let [firstName, lastName] = fullName
console.log(firstName)