forked from webdvt/js-cheat-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
162 lines (150 loc) · 3.78 KB
/
main.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// String, Numbers, Boolean, null, undefined
const name = 'John'; // String
const age = 15; // Number
const isLegal = false; // Boolean
const uberRating = 3.3; // Number
const x = null;
const y = undefined;
let z; // Undefined
console.log(typeof name);
console.log(typeof age);
console.log(typeof isLegal);
console.log(typeof uberRating);
console.log(typeof y);
// String concatenation
console.log("My name is " + name + " and I am " + age + " years old.");
// String Interpolation: lets you to inject variables & logic directly into a string
console.log(`My name is ${name} and I am ${age} years old.`);
// String operations and properties
const str = "Hello World";
console.log(`The length of s is ${str.length}`);
console.log(str.substring(0, 5)); // From index 0 to index 4 (2nd parameter exclusive)
let nums = "1,2,3,4,5";
console.log(nums.split(',')); // Splits string by delimiter
// Arrays
const integers = new Array(1, 2, 3, 4);
console.log(integers);
const fruits = ['apples', 'oranges', 'bananas', 'mangoes'];
console.log(fruits);
console.log(`The length of fruits is ${fruits.length}`);
const mixArr = ['1', 2, true]; // Array with different data types
console.log(mixArr);
// Accessing elements in array
console.log(fruits[1]); // index starts at 0
// Add element to array if you know array size
fruits[4] = 'grapes';
console.log(fruits);
// Add element to end of array if size is unknown
fruits.push('pears');
console.log(fruits);
// fruits = []; is illegal since it is being assigned to a different array
// Add element to beginning of array
fruits.unshift('strawberries');
console.log(fruits);
// Remove last element in array
fruits.pop();
console.log(fruits);
// Remove from beginning of array
fruits.shift();
console.log(fruits);
// Remove element at specific index
fruits.splice(0,0);
console.log(fruits);
// Remove range of elements
fruits.splice(1, 3);
console.log(fruits);
// Object Literals
let person = {
firstName: 'John',
lastName: 'Doe',
age: 70,
hobbies: ['hiking', 'drinking', 'science', 'inventing'],
address: {
street: '123 main st',
city: 'Blacksburg',
state: 'Virginia'
}
};
console.log(person);
// Access single value
console.log(person.firstName);
console.log(person.hobbies[2]);
console.log(person.address.street);
// Destructuring - create variables
const {firstName, lastName, address: {state}} = person;
console.log(firstName, lastName, state);
// Add property to object
person.email = '[email protected]';
console.log(person);
// For loops
const todos = [
{
id: 1,
text: 'take out trash',
isComplete: true
},
{
id: 2,
text: 'finish hw',
isComplete: false
},
{
id: 3,
text: 'meet with adviser',
isComplete: false
}
];
// set iterator value, set bound, set increment value
for (let i = 0; i < 10; i++) {
console.log(i);
}
// Array traversal
for (let i = 0; i < todos.length; i++) {
console.log(todos[i].text);
}
// For Each
for (let todo of todos) {
console.log(todo.id);
}
todos.forEach((todo) => {
console.log(todo);
});
// While loop
let counter = 0;
while (counter < 10) {
console.log(counter * counter);
counter++;
}
// Do while
counter = 0;
do {
console.log(counter * counter);
counter++;
}
while (counter < 10);
// Conditionals
counter = 0;
if (counter === 0) {
console.log('counter equals to 0');
}
else if (counter < 0) {
console.log('counter is negative');
}
else {
console.log('counter is positive');
}
// Functions
function isEven(x) {
return x % 2 === 0;
}
console.log(isEven(4));
console.log(isEven(3));
const mean = (set) => {
let sum = 0;
for (let i = 0; i < set.length; i++) {
sum += set[i];
}
return sum / set.length;
};
console.log(mean([1, 2, 3]));
console.log(mean([3,5,3,2,3,4,5]));