Skip to content

Commit 6e8d8a3

Browse files
fix: variables.js file
1 parent 2cac0c0 commit 6e8d8a3

File tree

1 file changed

+38
-83
lines changed

1 file changed

+38
-83
lines changed

part1 (Basics)/01_variables.js

Lines changed: 38 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,56 @@
1-
// using let (recommended for changeable values)
2-
let age = 20; // Declared and initialized with value 20
3-
age = 30;
1+
// variables.js
42

3+
// Using let (recommended for variables that can change)
4+
let age = 20; // Declare and initialize variable
5+
age = 30; // Update the value
56

6-
// using const (recommended for constant values)
7-
const pi = 3.14; // Declared and initialized. Value can't be changed.
87

9-
10-
// using var (old way, Avoid using it), var also add itself to the window object
11-
var name = "John";
12-
13-
14-
15-
// NOTE: Quizzez Time
16-
17-
// INFO: First Quizz
18-
let a = 10;
19-
let b = 10;
20-
console.log(a == b); // true
21-
console.log(a === b); // true
22-
23-
// INFO: Second Quizz
24-
let x = 3;
25-
console.log(x++); // 3
26-
console.log(++x); // 4
27-
28-
29-
// INFO: Idenfity the primitive data types
30-
let name = "Alice";
31-
let age1 = 30;
32-
let isHappy = true;
33-
let user = { name: "Bob" };
8+
// Using const (for constant values that should not change)
9+
const pi = 3.14; // Declare and initialize constant
3410

3511

36-
// INFO: What will be the output
37-
console.log(typeof NaN); // number
38-
console.log(typeof null); // object
39-
40-
41-
// INFO: What will be the ouput of the following code
42-
let c = 10;
43-
let e = c++;
44-
let d = ++c;
45-
console.log(c, e, d); // 12, 10, 11
46-
47-
48-
// INFO: What will be the output of the following code ?
49-
console.log(typeof NaN); // number
50-
console.log(NaN === NaN); // false
51-
console.log(isNaN("Hello")); // true
52-
53-
54-
// INFO: What will be the printed to the console?
55-
let z = "10";
56-
let u = 5;
57-
console.log(x + y); // 105
58-
console.log(+x + y); // 15
59-
12+
// Using var (old way, avoid if possible)
13+
// var declarations are function-scoped and can lead to unexpected behavior
14+
var name = "John";
6015

61-
// INFO: What will be the output of the following code ?
62-
let person = { name: "Alice" };
63-
let anotherPerson = person;
64-
anotherPerson.name = "Bob";
65-
console.log(person.name); // "Bob"
6616

17+
// Variable scope examples:
6718

68-
// INFO: What is wrong with this code snippet?
69-
const score;
70-
score = 100;
71-
console.log(score); // Syntax Error
19+
// Block scope with let and const
20+
if (true) {
21+
let blockScoped = "I exist only inside this block";
22+
const constantScoped = "Also block-scoped";
23+
console.log(blockScoped); // Works here
24+
}
25+
// console.log(blockScoped); // Error: blockScoped is not defined
7226

7327

74-
// INFO: What will be the result of this operation ?
75-
let num = 0.1 + 0.2;
76-
console.log(num === 0.3); // false
28+
// var is function scoped, not block scoped
29+
if (true) {
30+
var functionScoped = "I exist inside the function or globally";
31+
}
32+
console.log(functionScoped); // Works here (if not inside a function)
7733

7834

79-
// INFO: What will be the output of the following code ?
80-
let n = null;
81-
let i;
82-
console.log(typeof x); // object
83-
console.log(typeof y) // undefined
35+
// Variable naming rules:
36+
// - Can contain letters, digits, underscores, and dollar signs
37+
// - Cannot start with a digit
38+
// - Case sensitive (age and Age are different)
8439

40+
// Example of valid variable names:
41+
let userName;
42+
let $amount;
43+
let _count;
8544

86-
// INFO: What will be the output of the following code ?
87-
console.log([] + []); // ""
88-
console.log([] + {}); // [object object]
89-
console.log({} + []); // [object object]
45+
// Example of invalid variable name:
46+
// let 1stName; // Syntax Error
9047

9148

92-
// INFO: What is the difference between these two?
93-
let main = 0;
94-
let actual = new Number(0);
95-
console.log(main == actual); // false
96-
console.log(main === actual); // true
49+
// Variables declared without let, const, or var become global (avoid this)
50+
function wrongExample() {
51+
someVar = 10; // Declares a global variable unintentionally
52+
}
53+
wrongExample();
54+
console.log(someVar); // 10 (global scope)
9755

9856

99-
// INFO: What will be the output of this code ?
100-
console.log(0.1 + 0.2 === 0.3);
101-
console.log(0.1 + 0.2); // false, 0.30000000000000004

0 commit comments

Comments
 (0)