-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchap2.js
More file actions
144 lines (131 loc) · 2.58 KB
/
chap2.js
File metadata and controls
144 lines (131 loc) · 2.58 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
// if else
/*
let grocerySpending=300;
if (grocerySpending > 1000)
{
let discount=0.10 * grocerySpending;
console.log("You get 10% discount ", discount);
}
else if(grocerySpending > 500)
{
let discount=0.05 * grocerySpending;
console.log("You get 5% discount", discount);
}
else
{
console.log("No discount");
}
*/
//Switch statements
/*
let shippingOption = "standard";
switch (shippingOption)
{
case 'standard':
console.log("Your order will be delivered in 4-5 days");
break;
case 'one-day':
console.log("Your order will be delivered in one day");
break;
case 'fast':
console.log("Your order will be delivered in 2-3 days");
break;
default:
console.log("Invalid shipping option");
break;
}
*/
//Control statement
//let sum=0;
//let items=[10,30,20,70,50,40];
//For loop
/*
for(let i=0;i<items.length;i++)
{
sum=sum+items[i];
}
console.log(sum);
*/
//While loop
/*
i=0;
while (i<items.length)
{
sum=sum+items[i];
i++;
}
console.log(sum);
*/
//Function without argument
/*
function printIt()
{
console.log("Madhav, a frontend developer");
}
printIt();
*/
//funtion with argument
/*
function printname(name)
{
console.log(name);
}
console.log("COSMIC365.AI");
*/
//function using return
/*
function returnIt(name)
{
return name; // it will not execute anything so we have to save what it is returning.
}
let ans=returnIt("Main flow");
console.log(ans);
*/
//Scoping :- till where i can exist
/*
let a=5; // global scope :- can be accessed anywhere in the whole file
function sum()
{
let b=10; //block scope :- can be accessed inside the block only
console.log(a);
console.log(b);
}
sum();
console.log(a);
// console.log(b); //will show error like b is not defined bcz it's block scope
*/
//checking global and block variables
/*
{
var a=19; //global scope
let q=12; //block scope
const t=10; //block scope
}
console.log(a); //a will be accessed
console.log(q); //q will not be accessed
console.log(t); //t will not be accessed
*/
//checking global and block variables using function
/*
function we()
{
var a=10;
}
console.log(a); //will show error
*/
//scoping chain
/*
let a=10;
function outer()
{
let b=20;
function inner()
{
let c=30;
console.log(a ,b ,c); // it will check variable in inner, then outer, then global if still it's not find variable then will show that variable is not defind
//and for this 'a' and 'b' will be lexical scope variable
}
inner();
}
outer();
*/