-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctioning.js
More file actions
100 lines (76 loc) · 2.66 KB
/
Copy pathfunctioning.js
File metadata and controls
100 lines (76 loc) · 2.66 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
//functions use the keyword 'function'
//function syntax:
// function name(){
//}
//JavaScript allows for 'hoisting' which is where
//it allows access to function declarations before they're
//defined.
//example:
greetWorld();
function greetWorld(){
console.log('Hello, World!')
}
//greetWorld was called before it was defined
//this is not generally considered a good practice
//but you should be aware it can do this.
//also like java, parameters are placed within the parenthesis
//but in javascript, no type delcaration is needed
function example(name, age){
console.log(name+' is my name and I am '+age+' years old.')
}
example('Bee',24)
//default parameters can exist
//here is an example where the default parameter is
//stranger unless someone enters a name when the function
//is called.
function greeting(name='stranger'){
console.log(`Hello, ${name}!`)
}
greeting('Humi')
greeting()
//returns in a function work exactly like you expect them to
function isAdult(name,age){
if(age>=18){
return name+' is an adult!'
}else{
return name+'is not an adult yet.'
}
}
console.log(isAdult('Adrian',20))
//you can also define functions as related to a constant, this is called a function expression
//this is how you create a function expression:
const isAlright = function(happy,free){
if(happy&&free){
return true
}else{
return false
}
}
//heres how you call it
console.log(isAlright(true,true))
//you can also create functions by using "fat arrow" notation
//heres what that looks like
const rectangleArea = (width,height) => {
let area=width*height
return area
}
//this removes the need to type out the keyword function
//every time you need to create a function
//functions that take only one parameter do not need parentheses
const functionName = () => {}
//functions that take zero or multiple parameters need parentheses
const functionName2 = paramOne => {}
const functionName3 = (paramOne, paramTwo) => {}
//a function body of only a single line doesnt need {}
const sumNumbers = number => number+number
//without {}, whatever that line evaluates will be automaticlly returned
//this is called an implicit return
//blocks in javascript are anything between {}
//this can affect variable scope
//variables declared outside a block have global scope
//variables declared inside a block have block scope (local variables)
//trying to access a variable globally when it only has block scope results
//in a 'ReferenceError'
//too many global variables is called 'Scope Pollution'
//we want to avoid this because those variables will remain there until the program finishes
//making our global namespace fill up quickly