-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnote.txt
More file actions
218 lines (195 loc) · 4.18 KB
/
Copy pathnote.txt
File metadata and controls
218 lines (195 loc) · 4.18 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
Hoisting and scoping
Objects and JSON
Arrays and Array Methods.
Hoisting & scoping:-
=> If we a print a variable without declaration it leads to Hoisting
=> In var it will print undefined
=> In let and const it will throw error
=> It is a developer mistake
=> Let and const block variables
=> var global variables
Example for hositing:-
console.log(a);
const/let/var a =10;
Example for scoping:-
function test() {
let a=10;
if (true) {
let b = 20;
console.log(a);
}
console.log(b);
}
test();
Array :-
=>Collection of datatypes
=>It will be in sequence manner.
=>Enclosed within []
=>Index always starts with 0
=>To print the values inside the array we have use
arrayname[indexvalue]
=>To find the length dynamically we have to use
arrayname.length
=>To find the last index in an array we have to use
arrayname.length-1
Example:-
var a =[1,2,3,"abc",true]
for(var i=0;i<a.length;i=i+1){
console.log(a[i]);
}
Objects:-
=>Objects are structure with key value pair
=> Objects do not have index
syntax:-
var/let/const objectname = {
keyname:value
}
=> Accessing the element inside the object we have 2 methods
1.Dot method
2.Box method
=>Dot method
syntax:-
Objectname.Keyname
=>Box method
synatx:-
Objectname["Keyname"]
=>Example
Objects example using dot method
const person = {
name: "rupan",
age: 15,
gender: "male",
phonenumber: 9876543210,
skills: ["js", "html", "css", "react", "node", "mongodb"],
}
console.log(person);
console.log(person.name);
console.log(person.age);
console.log(person.gender);
console.log(person.phonenumber);
for(let i=0;i<person.skills.length;i=i+1){
console.log(person.skills[i]);
}
Objects example using box method
const person = {
name: "rupan",
age: 15,
gender: "male",
phonenumber: 9876543210,
skills: ["js", "html", "css", "react", "node", "mongodb"],
}
console.log(person);
console.log(person["name"]);
console.log(person["age"]);
console.log(person["gender"]);
console.log(person["phonenumber"]);
for(let i =0;i<=person["skills"].length-1;i=i+1){
console.log(person["skills"][i]);
}
For in loop:- loop over the keys
syntax:-
for(let key in objectname){
/block code
}
example:-
let arr = ["a","b","c"]
for(let key in arr){
console.log(key);
}
let str = "strn"
for(let key in str){
console.log(key);
}
let obj = {
a:1,
b:2,
c:3
}
for(let key in obj){
console.log(key);
}
const person = {
name: "rupan",
age: 15,
gender: "male",
phonenumber: 9876543210,
skills: ["js", "html", "css", "react", "node", "mongodb"],
}
for(let key in person){
console.log(key);
}
For of loop:- Loop over the values
syntax:-
for(let variable of iterable){
//block of code
}
For each loop :- It used to print the values
example:-
const person = {
name: "rupan",
age: 15,
gender: "male",
phonenumber: 9876543210,
skills: ["js", "html", "css", "react", "node", "mongodb"],
}
for(let key in person){
console.log(person[key]);
}
person.skills.forEach(ele=>{console.log(ele)})
// let arr = ["a","b","c"]
// arr.forEach(ele=>{console.log(ele)})
=>Adding,updating,Deleting the elements in objects
Adding a new element in object
syntax:- Objectname.keyname = value
Updating the existing element in object
syntax:- Objectname.keyname = value
Deleting the element in objectname
syntax:- delete objectname.keyname
Examples:-
const person = {
name: "rupan",
age: 15,
gender: "male",
phonenumber: 9876543210,
skills: ["js", "html", "css", "react", "node", "mongodb"],
}
console.log(person)
for(let key in person){
console.log(person[key]);
}
person.skills.forEach(ele=>{console.log(ele)})
Adding element in object
person.mailid="abc@gmail.com";
Updating the element in object
person.age=25;
Deleting the element in object
delete person.phonenumber;
JSON:-
=>Javascript Object Notation
syntax:
var/let/const objectname = {
"keyname":"value"
}
//Array Of Objects
//[{},{},{}]
const arr = [
{
"name":"John",
"age":"15"
},
{
"name":"Doe",
"age":"15"
},
{
"name":"Jack",
"age":"15"
},
{
"name":"Adam",
"age":"15"
}
]
for(let i=0;i<arr.length;i=i+1){
console.log(arr[i].name,arr[i].age);
}