-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
83 lines (64 loc) · 1.51 KB
/
functions.js
File metadata and controls
83 lines (64 loc) · 1.51 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
//Functions
function areaofrectangle(L,W)
{
var area = L * W
return area;
}
console.log(areaofrectangle(2,4))
function percprofit(SP,BP)
{
var profit = SP - BP;
var percentageprofit = (profit/BP) * 100
return percentageprofit;
}
console.log("%" + percprofit(500,200))
//writing reusable code using functions
function reusablefunc(){
console.log("Hello world")
}
reusablefunc()
reusablefunc()
//passing values to functions with arguments
function sub(a,b){
console.log(a-b)
}
sub(10,5)
//Global scope and functions
var myglobal = 10; //its global since its outside the function
function fun1(){
oopsglobal = 5// it becomes global since it has not been defined inside by use of var
}
function func2(){
var Output = " "
if(typeof myglobal != "undefined"){
Output += "myglobal: " + myglobal;
}
if(typeof oopsglobal != "undefined"){
Output += " oopsglobal: " + oopsglobal
}
return Output
}
console.log(fun1())
console.log(func2())
//reminding ourselve on checking data types
var type=typeof myglobal
console.log(type)
//local scope and functions
function mylocalscope(){
var myvar=5;
return myvar
}
console.log(mylocalscope())
//GLOBAL VS LOCAL SCOPE IN FUNCTIONS
var outerwear = "T-shirt"
function myoutfit(){
var outerwear = "Short"
return outerwear
}
console.log(myoutfit())
console.log(outerwear)
//Return a value from a function with return
function minusseven(num){
return num-7
}
console.log(minusseven(10))