1- // variables.js
2-
31// Using let (recommended for variables that can change)
4- let age = 20 ; // Declare and initialize variable
5- age = 30 ; // Update the value
6-
2+ let age = 20 ; // Declare and initialize variable
3+ age = 30 ; // Update the value
74
85// Using const (for constant values that should not change)
9- const pi = 3.14 ; // Declare and initialize constant
10-
6+ // NOTE: It's better to use all letters upppercase when using constants
7+ const PI = 3.14 ; // Declare and initialize constant
118
129// Using var (old way, avoid if possible)
1310// var declarations are function-scoped and can lead to unexpected behavior
1411var name = "John" ;
1512
16-
1713// Variable scope examples:
1814
1915// Block scope with let and const
2016if ( true ) {
2117 let blockScoped = "I exist only inside this block" ;
2218 const constantScoped = "Also block-scoped" ;
23- console . log ( blockScoped ) ; // Works here
19+ console . log ( blockScoped ) ; // Works here
2420}
2521// console.log(blockScoped); // Error: blockScoped is not defined
2622
27-
2823// var is function scoped, not block scoped
2924if ( true ) {
3025 var functionScoped = "I exist inside the function or globally" ;
3126}
32- console . log ( functionScoped ) ; // Works here (if not inside a function)
33-
27+ console . log ( functionScoped ) ; // Works here (if not inside a function)
3428
3529// Variable naming rules:
3630// - Can contain letters, digits, underscores, and dollar signs
@@ -45,12 +39,9 @@ let _count;
4539// Example of invalid variable name:
4640// let 1stName; // Syntax Error
4741
48-
4942// Variables declared without let, const, or var become global (avoid this)
5043function wrongExample ( ) {
51- someVar = 10 ; // Declares a global variable unintentionally
44+ someVar = 10 ; // Declares a global variable unintentionally
5245}
5346wrongExample ( ) ;
54- console . log ( someVar ) ; // 10 (global scope)
55-
56-
47+ console . log ( someVar ) ; // 10 (global scope)
0 commit comments