-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvariables.html
More file actions
211 lines (166 loc) · 4.93 KB
/
variables.html
File metadata and controls
211 lines (166 loc) · 4.93 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
<!-- in this lesson: -->
<!-- ------------------- -->
<!--
Lesson 5 : Variables
1. What is variable ?
2. Variable name restriction
3. Create a new variable using let
4. Re-assigning a value to a variable
5. 3 ways to create variable (let, const, var)
6. typeof + variable
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// 🟨What is variable ? --camelCase--
// Variable🟨 = 🟩Container
// 🟩 <- we can save a value
// 🟨Variable Name 🟥Restriction :-
// 🟦1. Cant use special words. Example: let❌
// ❌ let let = 5;
// 🟩 let let1 = 5;
// 🟦2. Cant start a variable name with number
// ❌ let 1name = 3;
// 🟩 let name1 = 5;
// 🟦3. Cant use special characters except: $_
// 🟩 let $name = 5;
// 🟩 let _name = 5;
// 🟨Create a new Variable using let
let variable1 = 3
console.log(variable1)
const calculation = 2 + 2
console.log(calculation)
console.log(calculation + 2)
const result = calculation + 2
console.log(result)
const message = "hello"
console.log(message)
console.log(";")
// 🟨Re-assigning a value to a variable
variable1 = 5
console.log(variable1)
// 🟨Syntax Rules for Re-assigning a value to a variable
variable1 = 5
console.log(variable1)
// 🟨increase a value of a variable by a certain number
variable1 = variable1 + 1
console.log(variable1)
// 🟨Naming Conventions
// 🟦1. camelCase : cartQuantity
// 🟦2. PascalCase : CartQuantity
// 🟦3. kebab-case : cart-quantity ❗(will not work in JavaScript)
// 🟨3 ways to create variable
// 🟦1. const variable2 = 3; (will not change in future)(Default)
const variable2 = 20
// 🟦2. var variable3 = 4; (has some issues)
var variable3 = 30
// 🟦3. let variable3 = 4; (will change in future)
let variable4 = 40
// 🟨typeof + variables
console.log(typeof message)
console.log(typeof variable2)
//🟨Variable Swapping:
let a = 5
let b = 10
// swap the values
;[a, b] = [b, a]
console.log("a : ", a)
console.log("b : ", b)
//🟨Increment and Decrement:
let count = 5
count++
console.log("Incremented count : " + count)
count--
console.log("Incremented count : " + count)
//🟨Calculate Area:
const length = 10
const width = 5
const area = length * width
console.log("Area of the rectangle : " + area)
//🟨Concatenating Strings:
var firstName = "Deep"
var lastName = "Kothari"
var fullName = firstName + lastName
console.log("Full Name : ", fullName)
//🟨Using Template Literals:
var firstName = "Deep"
var lastName = "Kothari"
var fullName = `${firstName} ${lastName}`
console.log(fullName)
//🟨Convert String to Number:
const numString = "42"
const num = Number(numString)
console.log("Original String : " + numString)
console.log("As a number : " + num)
console.log("Add 10 : " + (num + 10))
//🟨Variable Scope:
let globalVar = "I'am global"
function localVarScope() {
let localVar = "I'm local to the function"
console.log(localVar)
}
localVarScope()
console.log(globalVar)
//🟨Check Variable Type:
const value = true
if (typeof value === "string") {
console.log("It's a string")
} else if (typeof value === "number") {
console.log("It's a number")
} else if (typeof value === "boolean") {
console.log("It's a boolean")
} else {
console.log("It's something else")
}
//🟨Temperature Converter:
var fahrenheit = 68
var celsius = ((fahrenheit - 32) * 5) / 9
console.log(`${fahrenheit}°F is ${celsius}°C`)
//🟨Random Number Generator:
var randomNumber = Math.floor(Math.random() * 10) + 1
console.log("Random number : " + randomNumber)
//🟨Age Calculator:
const birthYear = 2003
const currentYear = new Date().getFullYear()
const age = currentYear - birthYear
console.log(age)
//🟨Variable Scope (Function vs. Block):
if (true) {
var varVariable = "I'm function-scoped"
let letVariable = "I'm block-scoped"
const constVariable = "I'm also block-scoped"
}
console.log(varVariable)
console.log(letVariable)
console.log(constVariable)
//🟨Simple Calculator:
const num1 = 10
const num2 = 5
const operator = "+"
let result1
switch (operator) {
case "+":
result1 = num1 + num2
break
case "-":
result1 = num1 - num2
break
case "*":
result1 = num1 * num2
break
case "/":
result1 = num1 / num2
break
default:
result1 = "Invalid operator"
}
console.log(`Result of ${num1} ${operator} ${num2} is ${result1}`)
</script>
</body>
</html>