-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_Functions.js
More file actions
143 lines (84 loc) · 3.12 KB
/
18_Functions.js
File metadata and controls
143 lines (84 loc) · 3.12 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// <<<<<<<<<<<<<<< FUNCTIONS >>>>>>>>>>>>>>>>
// Method 1 to define : Known as "EXPRESSION" : where a varaible is holding an function
const Data1 = function(){
console.log("the name is tarun")
console.log("age is 22 years")
console.log("learning web development")
}
Data1() // function call
// Method 2 to define
function Data2(){
console.log("the name is Aron")
console.log("age is 32 years")
console.log("learning music")
}
Data2()
// Adding two numbers by function
// method 1
function addTwoNum1(num1 , num2){ // num1 , num2 are parameters and value obtained by them are argumnets.
return (num1 + num2)
}
addTwoNum1(2,3) // return to ho gyi value pr print nhi hui kyo ki na to function mai print ka function hai or na he yaha
// method 2
function addTwoNum2(num1 , num2){
console.log(num1+ num2)
}
addTwoNum2(2,3) // value print hogi kyo ki function mai print ka functiuon used hai
// method 3
function addTwoNum3(num1 , num2){
return (num1 + num2)
}
console.log(addTwoNum3(2,3)) // value print hogi kyo ki yaha print ka function use hua hai
// Important NOTE :
function addTwoNum4(num1 , num2){
console.log(num1+ num2)
}
const result = addTwoNum4(2,3)
console.log("answer : " , result) // gives the undefined result
// kyo ki result mai value store he nhi ho rhi hai kyo ki function value return nhi kr raha hai.
// function kr raha hai bas printing .
function addTwoNum5(num1 , num2){
return (num1 + num2)
}
const Result = addTwoNum5(2,3) // return hoke ayi jo value vo store hui yaha
console.log("answer : " , Result) // or yaha pr hamne print karva di ( to result aa gya five)
// NOTE : anything written below retun will never be executed .
function loginUserMessage(username){
return `${username} just logged in `
}
console.log(loginUserMessage("tarun"))
// if no arrgument is passed in the () than we get undefined
console.log(loginUserMessage())
// Checking weather argument is given or not ( by the help of if - else)
function loginUserMessage2(username){
if(username === undefined){
console.log("please enter valid argument")
}
else
return `${username} just logged in `
}
console.log(loginUserMessage2())
// IMPORTANT NOTE : when we dont now how many parameters can be present in the function <<<< REST operator >>>>
function calculateCartPrice(...num){ // rest operator is used
return num // now kitne bhi arrguments aye ye num sab ko return kr dega array ki form mai
}
console.log(calculateCartPrice(200,400,600,56)) // sari value aa jayngui array ki form mai
// Note :
function calculateCartPrice2(val1,val2, ...num){
return num
}
console.log(calculateCartPrice2(200,400,600,56,7878))
// here val1 have value 200 val2 have value 400 and rest all the values are controlled by num.
// ++++++++++++++++++++++ INTERESTING ++++++++++++++++
// 1}
console.log(one(6))
function one(num){
return num + 1
}
// 2}
console.log(two(6))
const two = function(num){
return num + 1
}
// this last code will give error because when function is defined in the
// EXPRESSION way then we cannot accesss before its itialization.