-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
324 lines (215 loc) · 6.35 KB
/
Copy pathindex.js
File metadata and controls
324 lines (215 loc) · 6.35 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//alert("Hello World");
//one line comment
/*THis is how yo do a comment in js
External JS
We need a JS file
We need to link that JS file to our html. We still need to use
the script tag
*/
//alert("we are now using external JS");
/*
Variables
As of ES6, We do have two ways of creating a varaible and also one way for creating a constant.
original method for creating varibles is to use var
Syntax
var variableName;
Declaring variables with let
this was introduced in 2015 in ES6
let variableName;
*/
var num;
let num2;
//Assignment statements
num=9;
num2=10;
//Variable initialization statement
let total=num+num2;
const PI=3.14;
let name1='"I like programming". HE said';
/*
Escape characters
\' single quote
\" double quote
\\ backward slash
\n new line
*/
console.log(name1);
//console.log(num);
/*
Data Types in Java Script
Primitive types
1.String e.g. "THis is a string"
A string is a sequence of characters that are enclosed in single quotes or double quotes
2. Number e.g. 12/Infinity/3.14 In JS, akk numbers are floats
3.Boolean=> true or false
4.null
5.undefined
Objects e.g. {key:value},/ array[1,2,4], function
null vs undefined
undefined this means a variable has been declared but has not been assigned a value yet.Also
if a function deos not explicitly return a value, then it ends up returning undefined
null this is an assignment value. THis is used to represent the absence of a vaule. In JS null means nothing
*/
let sum;
console.log(sum);
/*
Type coercion:
The process of converting one data type to another. THis can be implicitly or explicitly
JS is a weakly-typed language
2+2=4
2+'2'="22"
'2'+2="22"
'2'+'2'='22'
'2'-0=2
*/
/*Functions
A function is a block of code that is desgined to perdorm an particular task.
A function ha a name.
and a function is executed when something invokes it.
Because of ES6, we have three different ways of declaring functions in Java Script
1. function keyword decalration
2. function expression saved in a variable
3. ES6 Arrow functions
The function keyword declaration
with this, a function is defined with teh function keyword, followed by the function name, followed by parentheses()
Function name can contain letters,digits,underscores, dollar signs. A function name cannot start with a number
function functionName(parameter1,parameter2,parameter3..parameterM){
function body. this is the code to be executed when the function is called
In java, this is a method.
}
*/
function test(){
alert("hello world");
}
function getSum(num1,num2){
return num+num2;
}
/*
Hoisting
Hoisting is JS's default behaviour of moving declarations to the top.
Whn using function functionName(parameters){...} to declare a function,
this function will hoist to the top of the script.
*/
// to call a function. you just use the function name and arguments
//test();
getSum(90,20);
x=10;
console.log(x);
var x;
/*
In Js, a varaibale can be declared after it has been used.This means a
variable can be used before it has been declared.
Function expressions (function expression saved in a variable)
A function can also be defined using an expression.
A function expression can be stored into a variable.
After a function has been stored into a variable, the variable can be used
as a function
let variableName=function(parameters){code to be executed goes here};
variableName(arguement);
*/
let getTotal=function(a,b,c){return a+b+c};
let y=getTotal(1,3,12);
console.log(y);
/*
Arrow functions
Arrow functions is another new way of creating functions. This was added in ES6
Arrow functions allow us to write a shorter function syntax.
Syntax for arrow functions
arrowFunctionName=(parameters)=>{
code to be executed goes here
}
Given the following function expression,
let hello =function(){
alert("hellow world)");
}
convert it into an arrow function.
solution
*/
let hello=()=>{
let carName="Toyota";//This is a local variable
// alert("hello world"+ carName);
}
/*Global variables
Variables declared outside of any function have global scope.
*/
//console.log("otuside"+carName)
//hello();
/*
Scope
Local scope and global scope
Local JS Variables
Variables declared within a function, they are local to the funciton
Local variables have a function scope. Local variables can only
be accessed witht he function where they are declared.
Arrays
Arrays are used to hold a collection of data and in JS arrays can be able to hold data of different types.
let arrayName=[];//This is an empty array
let arrayName1=[value1,value2,value3,..]
to change a value
arrayName[index]=newValue;
*/
let cunySchool=["LC","CCNY","BCC","Hostos","Hunter",1];
/* to access vlues from an array, you need the array name and an index.
arrayName[index]
*/
console.log(cunySchool[0]);
console.log(cunySchool.length);
cunySchool[0]="Lehman College";
console.log(cunySchool[0]);
console.log(cunySchool[cunySchool.length-1]);
/*
Objects
This is a way of organizing data using key/value pairs
Objects can contain many values
*/
const car={make: "Toyota",
model: "Matrix"};
/*
Accesing data from objects
bracket notation
obejctName["key"]
dot notation
objectName.keyName
*/
//bracket notation
console.log(car['make']);
//dot notation
console.log(car.model);
/*
Functions
Higher order functions
A higher order funtioon is a function that operaties on any other function
either as an argurments(actual parameters) or return them
function outerFunc(cb){
return cb();
}
From the example above which one is the higher order funtion
OuterFunc. This is because the outerFunc takes in cb and returns the call of the cb inside it
Callback Function
A callback function is any functipon that is passed into anoher function as an arguments,
which is then called inside the outer function to complete some action
Callback functions can be declared as functions, function expressions, or even anonymous
functions.
function outerFunc(cb){
return cb();
}
What is callback function in the above example?
cb()is the callback function and outerFunc is the higher order function
*/
function firstFunction(){
displayAlert("hello one");
}
function secondFunction(){
displayAlert("Hello two");
}
function displayAlert(text){
alert(text);
}
function calculate(num1,num2, callBackFunction){
var t=num1+num2;
callBackFunction(t);
}
secondFunction();
firstFunction();
calculate(30,20, displayAlert);
calculate(110,100, displayAlert);