-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.txt
135 lines (130 loc) · 3.01 KB
/
notes.txt
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
//functions
//types of functions
//var,let and const
//hoisting
//block and scope
//template literals
//spread opertaor
//rest parameter
-------------------------------
1.functions:Set of statements that performs specific task
syntax: function functionname(parameter){
//set of statements that perform specific task
}
functionname(); //function call
example:
function mul(a,b){
console.log(a*b)
}
mul(1,2);
//functions always have return
//Return: To reduce the number of times code executed.
//example:
function mul(a,b,c){
return a*b*c
}
console.log(mul(1,2,3));
//functionname is userdefined
//if print without return then it shows undefined
//function is used for DRY codes.
//one function will have only one return keyword.
//print the odd numbers in an array
var arr = [12,13,14,15,16];
var result =[];
//function is used to extract the values(odd number)
function odd(arr){
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
return result;
}
console.log(odd(arr));
--------------------------------
Types of function:
1.anonyomus function
2.IIFE(Immediately Invoked Function expression)
3.arrow function = ES6
-----------------
1.anonyomus function : a function without name.
function(){
//set of statements
}()
example:
var arr = [12,13,14,15,17,19,21]
var result =[];
var odd = function(arr){
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
return result;
}
console.log(odd(arr));
---------------------------------------------------
2.IIFE:
(function)(functioncall)
example
( function odd(arr){
var result =[];
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
console.log (result);
}
)([12,13,14,15,17,19,21])
-------------------------------------------------------
3.Arrow functions: introduced in ES6
it has a => keyword and return keyword is must.
function a(){
}
this has been overcomed as
var a = (arr)=>{
}
example:
var odd = (arr)=>{
var result =[];
for(var i=0;i<arr.length;i++){
if(arr[i]%2!=0){
result.push(arr[i]);
}
}
return result;
}
console.log(odd([12,13,14,15,17,19,21]))
---------------------------------------------
//function without parameter
function greet(){
console.log("welcome");
}
greet();
-----------------------------------------------
//template literals
template literals help us to overcome concatenation
`${expression}`
//spread operator : converts a string into character array
//synatx: ...
//example1
var arr=["guvi"]
console.log(...arr);
//example 2:
var arr1=["bob","join","sofia"]
var arr2 = ["rupan" , "guvi"]
var arr3 = [...arr2 , ...arr1];
console.log(arr3)
//rest parameter : it is used inside the function
//to consider remaining values
function foo(a,b,...rest){
var sum =0;
for(var i=0;i<rest.length;i++){
sum = sum+rest[i];
}
return sum;
}
console.log(foo(12,13,14,15,16,17))
//a=12
//b=13