Repository navigation
Expand file tree
/
Copy pathfunctions.js
More file actions
65 lines (48 loc) · 1.7 KB
/
Copy pathfunctions.js
File metadata and controls
65 lines (48 loc) · 1.7 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
// There are a few ways to create functions
// This is global because it is in a top-level scope
function myGlobalFunction() {
console.log("I'm a global function!");
}
myGlobalFunction();
console.log("\n\n=========\n\n");
function printThreeMessages(message1, message2) {
console.log(message1);
console.log(message2);
myGlobalFunction();
}
printThreeMessages("Hello, world!", "I'm in a good mood.");
function printUpToTwoMessages(message1, message2) {
if (message1) {
console.log(message1);
if (message2) {
console.log(message2);
}
}
}
printUpToTwoMessages(null, "Hello");
console.log("Notice how the previous function did not print anything?");
printUpToTwoMessages("I don't have to put in a second argument");
// We try and name our functions as verbosely as possible; this is called DAMP Programming.
// There's another way to use functions; we can store them inside of variables like so!
// We can store functions in variable as well!
// These are anonymous functions; they have no function name
var myFunction = function() {
console.log("Hello!");
};
var myOtherFunction = function() {
console.log("world");
};
myFunction();
myOtherFunction();
// Let's take a look at callbacks
// A callback is when your function takes another function, which it will run at some point later
function multiplyByTenAndSendToCallback(baseNumber, callback) {
var theEndResult = baseNumber * 10;
// We pass result to the callback
callback(theEndResult);
}
// theEndResult is going to be passed over through the result parameter
multiplyByTenAndSendToCallback(25, function(result) {
console.log("Is the result 250?");
console.log(result === 250);
});