Skip to content

Commit c7a9751

Browse files
committed
ES6 scope,object and array destructing, tmplete literals
1 parent 512c87d commit c7a9751

File tree

7 files changed

+262
-0
lines changed

7 files changed

+262
-0
lines changed

22. ES6/img/image.png

357 KB
Loading

22. ES6/index.html

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>ES6</title>
8+
<link href="https://fonts.googleapis.com/css2?family=Fredoka:wght@300..700&display=swap" rel="stylesheet">
9+
<link href="https://fonts.googleapis.com/css2?family=Caveat:wght@400..700&display=swap" rel="stylesheet">
10+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
11+
<style>
12+
* {
13+
margin: 0;
14+
padding: 0;
15+
}
16+
</style>
17+
</head>
18+
19+
20+
21+
22+
<body>
23+
24+
<div class="container not_found"></div>
25+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
26+
<script src="js/objectdestructing.js"></script>
27+
<!-- <script src="js/arraydestructing.js"></script> -->
28+
<!-- <script src="js/templeteLiteral.js"></script> -->
29+
<!-- <script src="js/arrowFunction.js"></script> -->
30+
<!-- <script src="js/scope.js"></script> -->
31+
<p></p>
32+
</body>
33+
34+
</html>

22. ES6/js/arraydestructing.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Array Destructuring
2+
3+
let fruits = ["Apple", "Banana", "Cherry", "Date"];
4+
5+
// If you need access elements from we can follow
6+
let fruit1 = fruits[0];
7+
let fruit2 = fruits[1];
8+
let fruit3 =fruits[2];
9+
10+
console.log(`Fruits: ${fruit1}, ${fruit2}, ${fruit3}`);
11+
12+
// but we array destructuring we can access array elements like array variables
13+
14+
// If you can access to elements exceed array this returns udefined
15+
16+
var [frt1, frt2, frt3, frt4, frt5] = fruits;
17+
console.log(`Fruits: ${frt1}, ${frt2}, ${frt3}, ${frt4}, ${frt5}`);
18+
19+
20+
// Now if we need array exclude some array Element we can access like that
21+
22+
var [frt1, frt2, , frt4 ] = fruits;
23+
console.log(`Fruits: ${frt1}, ${frt2}, ${frt4}`);
24+
25+
26+
// Swap array value
27+
// tradional way
28+
console.log(fruits);
29+
let temp =fruits[0];
30+
fruits[0] = fruits[1];
31+
fruits[1] = temp;
32+
33+
console.log(fruits);
34+
35+
// We can also swap value following technique
36+
[fruits[0], fruits[1]] = [fruits[1], fruits[0]];
37+
console.log(fruits);
38+
39+

22. ES6/js/arrowFunction.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Define arrow function
2+
// function hello(){
3+
// console.log("hello world!");
4+
// }
5+
6+
7+
8+
// Arrow Function
9+
let hello = ()=> {
10+
console.log("hello world!");
11+
}
12+
13+
hello();
14+
15+
// In only this senario we escape the parenthesis if function have one parameter
16+
hello = name => {
17+
console.log("Hello,", name + "!");
18+
}
19+
20+
hello("Aktar");
21+
22+
23+
// If there is only statement which is return do not need return statement
24+
// if write retrun it throw a error
25+
// or if we have a statement only we can escape {}
26+
cal = a => a ** a;
27+
console.log(cal(5));
28+
29+
30+
// if we write return we need to put {}
31+
cal2 = a => {return a * a}
32+
console.log(cal(3));
33+
34+
35+
//Callback function
36+
37+
numbers = [43,4,32,34,53,2];
38+
numbers.forEach(element => console.log(element));
39+
40+
41+
//Map
42+
let sq = numbers.map(element => element * element);
43+
44+
console.log(sq);

22. ES6/js/objectdestructing.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
let person = {
2+
firstName: 'Abdullah',
3+
lastName: 'Abdur Rahman',
4+
dob: "12-23-1998"
5+
};
6+
7+
// We can access it traditionally
8+
9+
var name = person.firstName + ' ' + person.lastName
10+
var dob = person.dob;
11+
12+
console.log(`Name: ${name}
13+
Date of Birth: ${dob}`);
14+
15+
// We can access it with object destructuring
16+
var { firstName, lastName, dob } = person;
17+
console.log(firstName + ' ' + lastName);
18+
console.log(dob);
19+
20+
// we need to same var object destructuring name same as object properties
21+
var { fname, lname, dob } = person;
22+
console.log(fname + ' ' + lname); //undefined cause use different name
23+
console.log(dob);
24+
25+
26+
// If we need change name we can use
27+
var { firstName: fname, lastName: lname, dob } = person;
28+
console.log(fname + ' ' + lname);
29+
console.log(dob);
30+
31+
32+
// we can use Object destruction in function
33+
34+
function printObject({ firstName, lastName, dob }) {
35+
console.log(fname + ' ' + lname);
36+
console.log(dob);
37+
}
38+
39+
console.log(firstName + ' ' + lastName);
40+
console.log(dob);

22. ES6/js/scope.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// We can redeclared variable before declared with var
2+
// It can create a serious problem in programming execution
3+
var a = 10;
4+
a = a + 7;
5+
6+
console.log(a);
7+
8+
var a = 10;
9+
console.log(a);
10+
11+
// We can solve the problem declared variable with let
12+
let b = 10;
13+
b = 30;
14+
console.log(b);
15+
16+
// let b = 39; // Cause create error
17+
18+
//When we create variable with const it can't be redeclared or reassigned
19+
const c = 10;
20+
21+
// Casuse an error
22+
// c = 45;
23+
//const c = 15;
24+
25+
console.log(c);
26+
27+
28+
//Scope
29+
// Local Scope
30+
31+
var p = 4;
32+
let q = 16;
33+
const r = 32;
34+
35+
console.log("Global Scope: ", p, q, r);
36+
37+
var x = 10;
38+
39+
function test() {
40+
var p = 2;
41+
let q = 8;
42+
const r = 12;
43+
x = 10;
44+
// We can access the global scope variable from the local scope
45+
console.log("Local Scope: ", p, q, r, x);
46+
47+
var z = 39;
48+
}
49+
50+
test();
51+
52+
if(true){
53+
// When we declared variable with var in if scope which redeclared before.
54+
// And reassign here here it can also be change in global scope
55+
// But let and const can not be changed in global scope
56+
var p = 2;
57+
let q = 8;
58+
const r = 12;
59+
}
60+
61+
console.log("Global Scope: ", p, q, r);
62+
//But you can not acces local scope value from
63+
// console.log(z);
64+
65+
// for scope work same as if scope
66+
67+
for(var i = 2; i <=10;i++){
68+
console.log(i);
69+
}
70+
71+
console.log(i);
72+

22. ES6/js/templeteLiteral.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Template Literal is a Feature of ES6
2+
// It use for write multiple comments
3+
4+
// For Write Multiple Line string we \n or write log statements
5+
// tow times
6+
console.log("Template Literal.");
7+
console.log("It is a Feature of ES6.");
8+
9+
10+
console.log("Template Literal.\nIt is a Feature of ES6.");
11+
12+
13+
// We can backtick `` use in write templte litral
14+
console.log(`Template Literal.
15+
It is a Feature of ES6.
16+
`);
17+
18+
let age = 26;
19+
let nam = "Abdullah";
20+
let dob = "13-04-1998";
21+
// we can write varible with in backtick ${}
22+
23+
console.log(`My Name is ${nam}
24+
My Age is:${age}
25+
My date of birt is:${dob}
26+
`);
27+
28+
// we can do arithmetic operations in template literals
29+
30+
let num1 = 10;
31+
let num2 = 20;
32+
33+
console.log(`Result: ${num1 + num2}`);

0 commit comments

Comments
 (0)