-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1.js
More file actions
97 lines (67 loc) · 3.67 KB
/
day1.js
File metadata and controls
97 lines (67 loc) · 3.67 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
// Activity 1: Variable Declaration
// Task 1: Declare a variable using `var`, assign it a number, and log the value to the console.
var numVar = 21;
console.log(numVar); // Output: 42
// Task 2: Declare a variable using `let`, assign it a string, and log the value to the console.
let strLet = "Hello World by Divyanshu";
console.log(strLet); // Output: Hello World by Divyanshu
// Activity 2: Constant Declaration
// Task 3: Declare a variable using `const`, assign it a boolean value, and log the value to the console.
const boolConst = true;
console.log(boolConst); // Output: true
// Activity 3: Data Types
// Task 4: Create variables of different data types and log each variable's type using the `typeof` operator.
let num = 100; // Number
let str = "30DaysofJavascriptByOCD"; // String
let bool = false; // Boolean
let obj = { name: "Divyanshu", Number: 7 }; // Object
let arr = [1, 2, 3, 4, 5]; // Array
console.log(typeof num); // Output: number
console.log(typeof str); // Output: string
console.log(typeof bool); // Output: boolean
console.log(typeof obj); // Output: object
console.log(typeof arr); // Output: object (Arrays are a type of object in JavaScript)
// Activity 4: Reassigning Variables
// Task 5: Declare a variable using `let`, assign it an initial value, reassign a new value, and log both values to the console.
let reassignVar = "Initial Value";
console.log(reassignVar); // Output: Initial Value
reassignVar = "New Value";
console.log(reassignVar); // Output: New Value
// Activity 5: Understanding `const`
// Task 6: Try reassigning a variable declared with `const` and observe the error.
const constVar = "Constant Value";
console.log(constVar); // Output: Constant Value
// Uncommenting the next line will cause an error
// constVar = "New Value"; // Error: Assignment to constant variable.
// Feature Request:
// 1. Variable Types Console Log: Write a script that declares variables of different data types and logs both the value and type of each variable to the console.
let numberType = 123;
let stringType = "This is a string";
let booleanType = true;
let objectType = { key: "value" };
let arrayType = [1, 2, 3, 4, 5];
let undefinedType;
let nullType = null;
console.log("Value: ", numberType, ", Type: ", typeof numberType);
// Output: Value: <value of numberType>, Type: number
console.log("Value: ", stringType, ", Type: ", typeof stringType);
// Output: Value: <value of stringType>, Type: string
console.log("Value: ", booleanType, ", Type: ", typeof booleanType);
// Output: Value: <value of booleanType>, Type: boolean
console.log("Value: ", objectType, ", Type: ", typeof objectType);
// Output: Value: <value of objectType>, Type: object
console.log("Value: ", arrayType, ", Type: ", typeof arrayType);
// Output: Value: <value of arrayType>, Type: object (Arrays are a type of object in JavaScript)
console.log("Value: ", undefinedType, ", Type: ", typeof undefinedType);
// Output: Value: <value of undefinedType>, Type: undefined
console.log("Value: ", nullType, ", Type: ", typeof nullType);
// Output: Value: <value of nullType>, Type: object (This is a well-known quirk in JavaScript)
// 2. Reassignment Demo: Create a script that demonstrates the difference in behavior between `let` and `const` when it comes to reassignment.
let letVariable = "I can be reassigned";
console.log(letVariable); // Output: I can be reassigned
letVariable = "I've been reassigned";
console.log(letVariable); // Output: I've been reassigned
const constVariable = "I cannot be reassigned";
console.log(constVariable); // Output: I cannot be reassigned
// Uncommenting the next line will cause an error
// constVariable = "Attempting reassignment"; // Error: Assignment to constant variable.