Skip to content

Commit 2157209

Browse files
authored
Add files via upload
1 parent 378f74c commit 2157209

20 files changed

+1430
-0
lines changed

021-Spread_and_Rest_Operator.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Author : Jaydatt Patel
3+
spread and rest operator : (...array) or (...array[size_limit])
4+
5+
In Javascript, both the spread operator and rest parameter have the same syntax which is three dots(...). Even though they have the same syntax they differ in functions.
6+
7+
Spread operator: The spread operator helps us expand an iterable such as an array where multiple arguments are needed, it also helps to expand the object expressions. In cases where we require all the elements of an iterable or object to help us achieve a task, we use a spread operator.
8+
spread operator used to:
9+
- Add new members to arrays without using the push() method,
10+
- Convert a string to an array and
11+
- Copy either an object or an array into a separate object
12+
13+
Rest operator: The rest parameter is converse to the spread operator. while the spread operator expands elements of an iterable, the rest operator compresses them. It collects several elements. In functions when we require to pass arguments but were not sure how many we have to pass, the rest parameter makes it easier.
14+
15+
*/
16+
console.log("---------0--------");
17+
const greeting = "Hello";
18+
const arrayOfChars = [...greeting];
19+
console.log(arrayOfChars); //  ['H', 'e', 'l', 'l', 'o']
20+
21+
console.log("---------1--------");
22+
const fruits = ['apple', 'pear', 'plum']
23+
const berries = ['blueberry', 'strawberry']
24+
const fruitsAndBerries = [...fruits, ...berries] // concatenate
25+
console.log(fruitsAndBerries); // outputs a single array
26+
27+
console.log("---------2--------");
28+
const flying = { wings: 2 }
29+
const car = { wheels: 4 }
30+
const flyingCar = {...flying, ...car} // concatenate
31+
console.log(flyingCar) // {wings: 2, wheels: 4}
32+
33+
console.log("---------3--------");
34+
let veggies = ['onion', 'parsley'];
35+
veggies = [...veggies, 'carrot', 'beetroot'];
36+
console.log(veggies);
37+
38+
console.log("---------4--------");
39+
const car1 = {
40+
speed: 200,
41+
color: 'yellow'
42+
}
43+
const car2 = {...car1}
44+
car1.speed = 201
45+
console.log('car1: ', car1.speed,', car2: ', car2.speed)
46+
47+
console.log("---------5--------");
48+
let obj = {
49+
key: 1,
50+
value: 4
51+
};
52+
let output = { ...obj };
53+
output.value -= obj.key;
54+
console.log(output.value);
55+
56+
console.log("---------6--------");
57+
function count(...basket) {
58+
console.log(basket.length)
59+
}
60+
count(10, 9, 8, 7, 6);
61+
62+
console.log("---------7--------");
63+
const myNames = ["Amit", "Rahul"];
64+
const bio = { ...myNames, runs: "codes.com" };
65+
console.log(bio);
66+
// { '0': 'Amit', '1': 'Rahul', runs: 'codes.com' }
67+
68+
//rest operator example
69+
console.log("---------8--------");
70+
function average(...args) {
71+
console.log(args);
72+
var avg =
73+
args.reduce(function (a, b) {return a + b;}, 0) / args.length;
74+
return avg;
75+
}
76+
console.log("average of numbers is : " + average(1, 2, 3, 4, 5));
77+
console.log("average of numbers is : " + average(1, 2, 3));

022-Objects.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Author : Jaydatt Patel
3+
Object literals and the dot notation
4+
One of the most common ways of building an object in JavaScript is using the object literal syntax: {}.
5+
6+
*/
7+
8+
console.log("--------Method-1---------");
9+
var student = {}; //create an object
10+
student.id = 123;
11+
student.name = "Rahul";
12+
student.degree = "BCA";
13+
student.subjects = ["C","C++","JAVA","Python"];
14+
student.printdata = function() //add function in objects
15+
{
16+
console.log(this);
17+
};
18+
student.getdata = function() //add function in objects
19+
{
20+
console.log(this.id,this.name,this.degree,this.subjects );
21+
};
22+
23+
student.printdata();
24+
student.getdata();
25+
26+
27+
console.log("--------Method-2---------");
28+
var address = {
29+
city: "Jaipur",
30+
pincode: 123456,
31+
state: "Rajasthan",
32+
contry: "India",
33+
show : function(){
34+
console.log(this.city,this.pincode,this.state,this.contry);
35+
}
36+
}
37+
address.show();
38+
console.log(address.city)
39+
address.city = "Ajmer";
40+
console.log(address)
41+
42+
/*
43+
Object literals and the brackets notation. There is an alternative syntax to the dot notation I used up until this point. This alternative syntax is known as the brackets notation.
44+
*/
45+
console.log("--------Method-3---------");
46+
var house = {};
47+
house["rooms"] = 4;
48+
house['color'] = "pink";
49+
house["price"] = 12345;
50+
console.log(house);
51+
52+
console.log("--------Method-4---------");
53+
var Keys = ['speed', 'altitude', 'color'];
54+
var drone = {
55+
speed: 100,
56+
altitude: 200,
57+
color: "red"
58+
}
59+
for (const k of Keys) {
60+
console.log(k,'-',drone[k])
61+
}
62+
63+
console.log("--------constructor-Method-5---------");
64+
//constructor functions for the built-in objects, I can also define custom constructor functions. (looks like class) same as above. No return type is required and it is optional..
65+
function Icecream(product) {
66+
this.flavor = product;
67+
this.melt = function() {
68+
console.log(`The ${this.flavor} icecream has melted`);
69+
}
70+
}
71+
let kiwi_Icecream = new Icecream("kiwi");
72+
let apple_Icecream = new Icecream("apple");
73+
kiwi_Icecream.melt();
74+
apple_Icecream.melt();
75+
76+
console.log("-------constructor-Method-6---------");
77+
//constructor functions for the built-in objects, I can also define custom constructor functions. (looks like class) same as above. No return type is required and it is optional..
78+
function createPerson(name) {
79+
const obj = {};
80+
obj.name = name;
81+
obj.introduceSelf = function () {
82+
console.log(`Hi! I'm ${this.name}.`);
83+
};
84+
return obj;
85+
}
86+
87+
const person = createPerson("Alex");
88+
person.name;
89+
person.introduceSelf();
90+
91+
92+
console.log("--------Method-8---------");
93+
// access target value by nesting keys of passed array of keys.
94+
const userProfile = {
95+
name: "John Doe",
96+
email: "john.doe@example.com",
97+
address: {
98+
city: "New York",
99+
street: "123 Main St",
100+
zipCode: "10001"
101+
}
102+
};
103+
104+
// access target value by nesting keys of passed array of keys.
105+
function getUserDetail(profile, keys) {
106+
let obj = profile;
107+
for(let key of keys){
108+
obj = obj[key];
109+
}
110+
return (obj != undefined) ? obj : 'Not available';
111+
}
112+
113+
console.log(getUserDetail(userProfile, ["address", "city"]));
114+
console.log(getUserDetail(userProfile, ["address", "block"]));
115+
116+
117+
//create new object from old object using prototype inheritance
118+
console.log("--------Method-8---------");
119+
var o1 = {}
120+
o1.obj1 = "Object-1";
121+
console.log("Object.getPrototypeOf(o1) :",Object.getPrototypeOf(o1));
122+
123+
var o2 = Object.create(o1); //create new obj with prototypes of old objects
124+
o2.obj2 = "Object-2";
125+
console.log("Object.getPrototypeOf(o2) :",Object.getPrototypeOf(o2));
126+
127+
var o3 = Object.create(o2);
128+
o3.obj3 = "Object-3";
129+
console.log("Object.getPrototypeOf(o3) :",Object.getPrototypeOf(o3));
130+
131+
console.log("o3.obj1 :",o3.obj1);
132+
console.log("o3.obj2 :",o3.obj2);
133+
console.log("o3.obj3 :",o3.obj3);
134+

023-Objects-functions.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Author : Jaydatt Patel
3+
Built-in methods: Object.keys(obj), Object.values(obj), and Object.entries(obj).
4+
5+
- Object.keys(obj) : return an array list of properties
6+
- Object.values(obj) : returns an array list values.
7+
- Object.entries(obj) : returns an array listing both the keys and the values.
8+
- Object.assign(target, source1, source2, …, sourceN) : update or overwrite object Properties from other object
9+
- Object.create(obj) : to create new object by inheriting object prototyps
10+
- Object.freeze(obj) : to create new object by static method freezes an object. Freezing an object prevents extensions and makes existing properties non-writable and non-configurable. A frozen object can no longer be changed: new properties cannot be added, existing properties cannot be removed, their enumerability, configurability, writability, or value cannot be changed, and the object's prototype
11+
- Object.getPrototypeOf(obj): to get the properties of object
12+
- Object.setPrototypeOf(target, source) : to set the properties of object
13+
- Object.hasOwn(obj, property) : check property exist or not
14+
15+
*/
16+
17+
18+
console.log("-----------------");
19+
const target = { a: 1, b: 2 };
20+
const source = { b: 4, c: 5 };
21+
const returnedTarget = Object.assign(target, source);
22+
console.log(target);
23+
console.log(returnedTarget);
24+
console.log(returnedTarget === target);
25+
console.log(returnedTarget == target);
26+
27+
console.log("-----------------");
28+
// copy object using Object.assign() Method
29+
var obj = { k :'world'}
30+
var copy = Object.assign({}, obj)
31+
console.log(obj);
32+
console.log(copy === obj);
33+
console.log(copy == obj);
34+
35+
console.log("-----------------");
36+
// copy object using Spread Method
37+
var obj = { k :'hello'}
38+
var copy = { ...obj }
39+
console.log(obj);
40+
console.log(copy === obj);
41+
console.log(copy == obj);
42+
43+
console.log("-----------------");
44+
const car2 = {
45+
speed: 200,
46+
color: "red"
47+
}
48+
console.log(Object.keys(car2));
49+
console.log(Object.values(car2));
50+
console.log(Object.entries(car2));
51+
52+
console.log("-----------------");
53+
function testBracketsDynamicAccess() {
54+
var dynamicKey;
55+
if(Math.random() > 0.5) {
56+
dynamicKey = "speed";
57+
}
58+
else{
59+
dynamicKey = "color";
60+
}
61+
var drone = {
62+
speed: 15,
63+
color: "orange"
64+
}
65+
66+
console.log("drone[dynamicKey] : ",drone[dynamicKey]);
67+
}
68+
testBracketsDynamicAccess();
69+
70+
71+
//create new object from old object using prototype inheritance
72+
console.log("---------------------------");
73+
var o1 = {}
74+
o1.obj1 = "Object-1";
75+
console.log('o1.obj1 : ',o1.obj1);
76+
console.log("Object.getPrototypeOf(o1) :",Object.getPrototypeOf(o1));
77+
78+
var o2 = Object.create(o1); //create new obj with prototypes of old objects
79+
o2.obj2 = "Object-2";
80+
console.log('o2.obj1 : ',o2.obj1);
81+
console.log('o2.obj2 : ',o2.obj2);
82+
console.log("Object.getPrototypeOf(o2) :",Object.getPrototypeOf(o2));
83+
84+
var o3 = Object.create(o2);
85+
o3.obj3 = "Object-3";
86+
console.log('o3.obj1 : ',o3.obj1);
87+
console.log('o3.obj2 : ',o3.obj2);
88+
console.log('o3.obj3 : ',o3.obj3);
89+
console.log("Object.getPrototypeOf(o3) :",Object.getPrototypeOf(o3));

024-Destruct_array_and_object.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Author : Jaydatt Patel
3+
Destructur the array or object property and copy them in other variable or object without affecting original data.
4+
*/
5+
console.log("--------------Destructure Array-------");
6+
7+
console.log("-----------1-----------");
8+
const meal = ["soup", "steak", "ice cream"]
9+
let [starter] = meal;
10+
console.log(starter);
11+
12+
console.log("-----------2-----------");
13+
const fruits = ['Apple','Mango','Kiwi','Berry','banana','lichi'];
14+
// skip elements by adding comma
15+
var [a, , , k] = fruits;
16+
console.log('[a, , , k]');
17+
console.log('a :', a,', k :',k);
18+
19+
var [a, m, ...arr] = fruits;
20+
console.log('\n[a, m, ...arr]');
21+
console.log('a :',a,', m: ', m,', arr :',arr);
22+
23+
var [a, m, ...[, be, ba]] = fruits;
24+
console.log('\n[a, m, [ , be, ba]]');
25+
console.log('a :',a,', m: ', m,', be :',be,', ba : ', ba);
26+
27+
console.log("-----------3-----------");
28+
29+
let nestedArray = ["orange", ["apple", "banana", ["grape", "mango"]], "peach"]; let [first, [ , ,[third]]] = nestedArray;
30+
console.log('\n[first, [ , ,[third]]]');
31+
console.log('first :',first,', third: ', third);
32+
33+
console.log("--------------Destructure Object-------");
34+
35+
var student = {}; //create an object
36+
student.id = 123;
37+
student.name = "Rahul";
38+
student.degree = "BCA";
39+
student.subjects = ["C","C++","JAVA","Python"];
40+
student.printdata = function() //add function in objects
41+
{
42+
console.log(student);
43+
};
44+
student.getdata = function() //add function in objects
45+
{
46+
console.log(this.id,this.name,this.degree,this.subjects );
47+
};
48+
49+
//get copy value of id property of student
50+
console.log("-----------1-----------");
51+
console.log((id === student.id)); // check the same property
52+
var {id} = student; //copy value of id from student obj if not found then undefined
53+
console.log(id);
54+
console.log((id === student.id));
55+
56+
//get values of multiple properties of object in local variable
57+
console.log("-----------2-----------");
58+
59+
var {id,name,subjects,xyz} = student; //copy from student obj if not found then undefined
60+
console.log('id :', id);
61+
console.log('name :', name);
62+
console.log('subjects :', subjects);
63+
console.log('xyz :', xyz);
64+
65+
//get values of multiple properties of object in local variable
66+
console.log("-----------3-----------");
67+
var {id : roll, name : nm , subjects : sub} = student; //copy from student obj if not found then undefined
68+
console.log('roll :', roll);
69+
console.log('nm :', nm);
70+
console.log('sub :', sub);
71+
console.log((roll === student.id)); // check the same property
72+
73+
//get copy of multiple property of object in new variable name
74+
console.log("-----------4-----------");
75+
var num, fullname, dg;
76+
({id : num, name : fullname , degree : dg} = student); //copy from student obj if not found then undefined
77+
console.log('num :', num);
78+
console.log('fullname :', fullname);
79+
console.log('dg :', dg);
80+
81+
//try to copy unkonwn property
82+
console.log("-----------5-----------");
83+
console.log((pop === student.id)); // check the same property
84+
var {pop} = student; //copy pop from student obj if not found then undefined
85+
console.log(pop);
86+
console.log((pop === student.id));

0 commit comments

Comments
 (0)