-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascriptBasics.js
More file actions
58 lines (50 loc) · 1.43 KB
/
Copy pathjavascriptBasics.js
File metadata and controls
58 lines (50 loc) · 1.43 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
//**********************************************
//You should pick either declaration or expression functions. Only use 1 of these types in your code:
//FUNCTION DECLARATION
function foo() {
console.log(“function declaration”);
}
//FUNCTION EXPRESSION - Assigns anonymous expression to a variable
var bar = function() {
console.log(“function expression”);
}
//(it is recommended that you start with expressions only)
//**********************************************
//ARGUMENTS VS PARAMTERS
//Here, input is an argument:
var bar = function(input) {
console.log(input);
}
//Here, x is a variable:
var x = ‘Hello world!’;
bar(x);
//**********************************************
//Does order matter with parameters?
// YES - IT DOES MATTER
bar(1, 2, 3);
// is different than
bar(3, 2, 1);
//**********************************************
//SCOPE
//Think of a pyramid. This is known as a "call stack."
//Inner functions can see variables in outer functions.
function a() {
var x = 5;
function b() {
var y = 10;
function c() {
var z = 15;
}
function d() {
console.log(x);//this equals 5
console.log(y);//this equals 10
console.log(z);//this is undefined
}
}
}
// In the above example, var z in function c is otuside of function d's scope.
//**********************************************
//METHOD VS FUNCTION
var obj = {};
obj.foo = function(){};//method
var bar = function(){};//function