Skip to content

Commit 02fa7f3

Browse files
committed
scope this and arrow
1 parent 50c7323 commit 02fa7f3

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

03_basics/2scopes.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
//var c = 300
3+
let a = 300
4+
if (true) {
5+
let a = 10
6+
const b = 20 //block scope
7+
// console.log("INNER: ", a);
8+
9+
}
10+
11+
//global scope
12+
13+
// console.log(a);
14+
// console.log(b);
15+
// console.log(c);
16+
17+
18+
//nested scope
19+
//child function can access the parent variable but not vice versa
20+
21+
function one(){
22+
const username= "Naini"
23+
24+
function two(){
25+
const website= "youtube"
26+
console.log(username);
27+
}
28+
// console.log(website); connot be accessed
29+
30+
two()
31+
32+
}
33+
34+
//one()
35+
36+
37+
if (true) {
38+
const username = "hitesh"
39+
if (username === "hitesh") {
40+
const website = " youtube"
41+
// console.log(username + website); will run
42+
}
43+
// console.log(website); error
44+
}
45+
46+
// console.log(username); error
47+
48+
49+
//++++++++++++ interesting +++++++++++++
50+
51+
console.log(addone(5)) //will run
52+
53+
function addone(num){
54+
return num + 1
55+
}
56+
57+
58+
59+
addTwo(5) //error if written before function declaration
60+
const addTwo = function(num){
61+
return num + 2
62+
}

03_basics/3arrow.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//this- refers to current context
2+
3+
const user = {
4+
username: "hitesh",
5+
price: 999,
6+
7+
welcomeMessage: function() {
8+
console.log(`${this.username} , welcome to website`);
9+
console.log(this);
10+
}
11+
12+
}
13+
14+
// user.welcomeMessage()
15+
// user.username = "sam"
16+
// user.welcomeMessage()
17+
18+
// console.log(this);
19+
20+
//global object in browser- windows
21+
22+
// function chai(){
23+
// let username = "hitesh"
24+
// console.log(this.username);
25+
// }
26+
27+
// chai()
28+
29+
// const chai = function () {
30+
// let username = "hitesh"
31+
// console.log(this.username);
32+
// }
33+
34+
const chai = () => {
35+
let username = "hitesh"
36+
console.log(this);
37+
}
38+
39+
// chai()
40+
41+
// const addTwo = (num1, num2) => {
42+
// return num1 + num2 //explicit return
43+
// }
44+
45+
// const addTwo = (num1, num2) => num1 + num2 //implicit return
46+
47+
// const addTwo = (num1, num2) => ( num1 + num2 )
48+
49+
const addTwo = (num1, num2) => ({username: "hitesh"})
50+
51+
52+
console.log(addTwo(3, 4))
53+
54+
55+
// const myArray = [2, 5, 3, 7, 8]
56+
57+
// myArray.forEach()

0 commit comments

Comments
 (0)