-
Notifications
You must be signed in to change notification settings - Fork 0
/
---ES6.js
248 lines (166 loc) · 7.64 KB
/
---ES6.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* ES6 review
ECMAScript 6 = 6th release of the ECMAScript language specification.
'use strict' = you can utilize ES6 in node and in some browsers.
------------------------------
The Ternary Operator:
ES6 Specific: (functions expressions)
*/
const capitalize = ( input )=> input.toUpperCase();
/* ^
Will automatically return -------|
However, on multiple lines you need the return:
*/
/*This declares the function
|| ||
\/ \/ */
const capitalize = ( input )=> {
input = input + '!!!';
return input.toUpperCase();
}
/*
(not ES6 specific)
*/
var someVariable = [condition] ? [if true] : [if false]
//some examples:
var simpleQuestion = 2>1 ? 'yes' : 'no';
console.log(simpleQuestion) //result: yes
//-----------------------------
var dayOfMonth = 21;
var takesOutTrash = isEven(dayOfMonth) ? 'Vin' : 'Karen';
console.log(takesOutTrash);
function isEven(dayOfMonth){ return dayOfMonth % 2 ===0;}
//-----------------------------
/*------------------------------
----> var, let and const
var = the variable is accessable within the entire function
let = block scope designator. let exists within the curly braces. It is a throw away variable.
const = is also a block scoping designator. It designates that you are setting a constant variable.
Once it is assigned, it cannot be changed. It is a read only reference to a value.
(it is & can only be a pointer only a pointer)
*/
const x=5;
x++; //This will fire an error and the code will not run
//because you are trying to change this constant
const y = [1,2,3,4,5];
y.push(6); //result: [1,2,3,4,5,6]
// y is a pointer to an object. Objects can always be changed.
y = [8,9,10]; //This will fire an error, because you are trying to change the pointer
//to point ot another object which is not a legal operation.
//The pointer y is a constant.
let y=5;
const x=y; //<---This assigns 5 to x
y=6; //<---y is independent of x, so y can still be changed
console.log(x); //result: 5
//-----------------------------
/*------------------------------
New String Methods:
*/
str.includes(str2);
str.repeat(count);
str.startsWith(str2);
str.endsWith(str2);
// some examples:
let str = 'VinceRios';
let str2 = 'HelloWorld';
let str3 = 'ince';
let str4 = 'Vin';
let str5 = 'os';
let count = 2;
console.log('str3 in str?:',str.includes(str3));
//Validates whether str contains str3
//result: true
console.log('str3 in str2?:',str2.includes(str3));
//Validates whether str contains str3
//result: false
//---------
console.log('str repeat 2*:',str.repeat(count));
//result: VinceRiosVinceRios
//---------
console.log('str starts with str4?:',str.startsWith(str4));
//Validates whether str starts with str4
//result: true
console.log('str2 starts with str4?:',str2.startsWith(str4));
//Validates whether str2 starts with str4
//result: false
//---------
console.log('str ends with str5?:',str.endsWith(str5));
//Validates whether str ends with str5
//result: true
console.log('str2 ends with str5:',str2.endsWith(str5));
//Validates whether str2 ends with str5
//result: false
//---------
/*------------------------------
New Number Methods:
*/
Number.isInteger(n); //This is a class (static) method.
// If you wanted to write this function yourself:
function isInt(n){
//first make sure the argument is a Number
if (typeof n !== 'number'){
console.log('Enter a number');
return false;
}
// the calculation
//let temp = Math.floor(n);
//return (n === temp) ? true : false;
// You could have also done this:
return (n % 1) ? false : true;
}
isInt(25); // returns: true
isInt(25.5); // returns: false
//---------
/*------------------------------
Arrow Functions:
We can replace anonymous functions with an arrow symbol (AKA: arrow function). '=>'
- Parameters go on the left of the arrow.
- The workings of the function go on the right of the arrow.
- If the workings stay on one line then no return is needed.
- If you use brackets, then you must use a return.
//---------
/*-------------------------------
example:
*/
.forEach()
let arr = [1,2,3,4];
function sumArray(arr){
let sum = 0;
arr.forEach((elem) => sum +=elem);
return sum;
}
console.log(sumArray(arr));
//------------
.filter()
let ages = [18, 20, 21, 25, 15];
function canDrink(arr){
return arr.filter((elem, index) => elem>=21);
}
console.log('canDrink: ', canDrink(ages)); //result: canDrink: [ 21, 25 ]
console.log('original array: : ', ages); //result: original array: : [ 18, 20, 21, 25, 15 ]
//-------------
.map()
let arr = [1,2,3,4];
//This is the usual way to do a .map call back:
let mappedArr = arr.map(function(elem){
return elem+1;
});
console.log(mappedArr); //result: [ 2, 3, 4, 5 ]
//This is the arrow function way:
let mappedArr = arr.map(elem => elem+1);
console.log(mappedArr); //result: [ 2, 3, 4, 5 ]
//------------
.reduce()
let reduceArr = arr.reduce((prev,next)=>prev+next);
console.log(reduceArr); //result: 10
//If you have to go more than one line:
reduceArr = arr.reduce((prev,next) => {
return prev+next});
console.log(reduceArr); //result: 10
//------------
.sort()
let people = [{name: 'corey', age: 24}, {name: 'mom', age: 70}, {name: 'dad', age: 77}, {name: 'doug', age: 30}];
people.sort((curr, next) => (curr.age - next.age));
console.log(people);
//result: [ { name: 'corey', age: 24 }, { name: 'doug', age: 30 }, { name: 'mom', age: 70 }, { name: 'dad', age: 77 } ]
//---------
//------------------------------------