-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.js
More file actions
60 lines (50 loc) · 1.39 KB
/
Copy pathmethods.js
File metadata and controls
60 lines (50 loc) · 1.39 KB
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
// Methods are a way to change data that you have created
// Each data type has methods built inside of it
// Methods are only used when you add () after their name
// String Methods
let aString = "My string"
aString.toUpperCase()
aString.toLowerCase()
aString.startsWith("M")
// Number methods
let myNumber = 1
myNumber.toPrecision()
// Array Methods
let myArray = []
// Push adds an item to an array
myArray.push('something')
// Pop removes the last item in the array
myArray.pop()
// length gives you the number of elements in the array
myArray.length
// Iterators go through every item
// and do something
// forEach just does something for each
// item in the array. In this case it will print
// each item
let myArray2 = [1, 2, 3, 4]
myArray2.forEach(
(item) => {
console.log(item)
}
)
// Map creates a new array in the background,
// and adds each thing that is returned to the
// new array. In this case, the numbers in
// myArray2 will have 1 added to them and put into
// the newArray. But, myArray2 will remain the same
let newArray = myArray2.map(
(item) => {
return item++
}
)
// Here, filter will also create a new
// array, which will not include any odd
// numbers. The old array will stay the same,
// but the new array will have only the items
// that return true
let myFilteredArray = myArray2.filter(
(item) => {
return item % 2 === 0
}
)