-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions-in-JavaScript.js
More file actions
62 lines (45 loc) · 1.71 KB
/
Functions-in-JavaScript.js
File metadata and controls
62 lines (45 loc) · 1.71 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
const func=function(){ // function defination
console.log("hello world")
}
func() //----> funtion call (A funtion will not execute unless it is called)
//--------------------------------------------------------------------------------------------------------------
// function to add two numbers
const sum=function(num1,num2){
const sum=num1+num2
return sum
}
const result=sum(2,3)
console.log("result is :- ",result)
/*
sum is a local variable in above sum() function.
It can not be accessed outside the scope of the function sum()
variables defined outside the sum() function are global variable.---> result
They can be accessed everywhere in code.
*/
const global_var="i am global variable"
const random_func=function(){
const local_var="i am local avriable"
console.log("global variable inside local scope :- ",global_var)
console.log("local variabla inside local scope :-",local_var)
}
random_func()
console.log("global variable outside local scope :- ",global_var)
//console.log("local variable outside local scope :- ",local_var) //--> gives error as local_var not defined
//--------------------------------------------------------------------------------------------------------------
// function to print the area of rectangle:-
const area_rec=function(len,bre){
const area=len*bre
return area
}
const num1=10
const num2=20
const area=area_rec(num1,num2)
console.log("area of rectangle with length ",num1," and width ",num2," is :- ",area)
// function to print the area of circle:-
const area_cir=function(radius){
const area=3.14 * radius *radius
return area
}
const num3=7
const ar=area_cir(num3)
console.log("area of circle with radius ",num3," is :- ",ar)