You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Before ES6 -> var was used -> After ES6 -> let is used
4
+
// const is used to define constant -> throughout the program value not changed
5
+
6
+
// Let, Const -> Used To Block Scope Variable Declaration
7
+
// Var -> Globally Scoped
8
+
// Let Can Be Updated But Not Re-declared
9
+
//Const -> Neither Updated Nor Re-declared
10
+
// Var -> Can Be Updated And Re-declared
11
+
// Var & Let Is Initialized With Undefined -> Const Are Not ->
12
+
vardp;
13
+
letap;
14
+
consthp;//Error
15
+
16
+
varmyVariable;// Declaration using var
17
+
letanotherVariable;// Declaration using let
18
+
constPI=3.14159;// Declaration using const
19
+
20
+
// JS is Case Sensitive
21
+
22
+
// You can also declare and assign a value to a variable in a single line.
23
+
24
+
//JavaScript Data-Type
25
+
//Object Is Non Primitive Data-Type
26
+
// Primitive -> Null, Number, String, Symbol, Undefined, Boolean, Bigint -> Fundamental Data-Type -> nn bb ss u
27
+
28
+
29
+
lety=BigInt("265");
30
+
letx=Symbol("I Am Symbol");
31
+
lets=null;//Define Null
32
+
33
+
// Type
34
+
console.log(typeofx);
35
+
36
+
//Object -> In Python -> Dictionary
37
+
//ES6 -> ECMAScript -> Modern JavaScript
38
+
constitem={
39
+
name: "CryptoMinds",
40
+
age: "12"
41
+
//Key: Value
42
+
}
43
+
44
+
console.log(item["age"]);//Look Up
45
+
46
+
//Scope -> Alt + Click -> Multiple Cursor In Replit
47
+
//var
48
+
49
+
varb=11;
50
+
varb=13;// Allow To Use
51
+
52
+
{
53
+
varb=15;
54
+
console.log(b);
55
+
}
56
+
console.log(b);
57
+
58
+
//Output
59
+
// 15
60
+
// 15
61
+
62
+
//let
63
+
64
+
letb=11;
65
+
66
+
{
67
+
letb=15;
68
+
console.log(b);
69
+
}
70
+
console.log(b);
71
+
72
+
//Output
73
+
// 15
74
+
// 11
75
+
76
+
77
+
letc=16;
78
+
c=17;//Update
79
+
80
+
81
+
letd=16;
82
+
letd=17;//Error
83
+
84
+
85
+
86
+
87
+
88
+
89
+
varmyNumber=42;
90
+
letmyString="JavaScript";
91
+
constmyConstant=true;
92
+
93
+
// Variable Naming:
94
+
// JavaScript variable names can include letters, digits, underscores, and dollar signs. They must start with a letter, underscore, or dollar sign (but not a digit). JavaScript is case-sensitive, so myVariable and myvariable are considered different variables.
95
+
96
+
varfirstName;
97
+
letage;
98
+
constPI=3.14;
99
+
100
+
101
+
102
+
// Better Practice to use let and Const
103
+
// Mostly Use Const
104
+
// JS Allow to Change Variable Type In Run Time -> Only One
105
+
106
+
107
+
functionmyFunction(){
108
+
varx=10;// Function-scoped variable
109
+
if(x>5){
110
+
lety=20;// Block-scoped variable
111
+
console.log(x+y);
112
+
}
113
+
console.log(x);// Accessible
114
+
console.log(y);// Error: y is not defined
115
+
}
116
+
117
+
118
+
// Variable Hoisting:
119
+
// JavaScript has a concept called "hoisting," where variable and function declarations are moved to the top of their respective scopes during the compilation phase. Variables declared with var are hoisted but are not initialized until the line where they are defined. This can sometimes lead to unexpected behavior.
120
+
121
+
122
+
console.log(x);// Undefined
123
+
varx=10;
124
+
125
+
126
+
// Variable Constants:
127
+
// Variables declared with const are constants, meaning their value cannot be reassigned once it is set. However, it's important to note that const does not make objects or arrays immutable. It only prevents the reassignment of the variable itself.
128
+
129
+
130
+
constPI=3.14;
131
+
PI=3.14159;// Error: Assignment To Constant Variable
132
+
133
+
constmyArray=[1,2,3];
134
+
myArray.push(4);// Valid, Since The Array Itself Is Mutable
135
+
136
+
// Conclusion:
137
+
// JavaScript variables play a crucial role in storing and manipulating data within a program. By understanding variable declaration, assignment, naming, scope, and the differences between var, let, and const, developers can effectively work with variables to create dynamic and interactive JavaScript applications.
0 commit comments