|
| 1 | +function box() { |
| 2 | + var a = "i am a"; |
| 3 | + console.log("Function Scope"); |
| 4 | + console.log(a); |
| 5 | +} |
| 6 | +box(); |
| 7 | + |
| 8 | +// Here Var a is working inside function scope |
| 9 | + |
| 10 | +// function box() { |
| 11 | +// var a = "i am a"; |
| 12 | +// console.log("Function Scope"); |
| 13 | +// } |
| 14 | +// box(); |
| 15 | +// console.log(a); |
| 16 | + |
| 17 | +// VAR USES |
| 18 | + |
| 19 | +// 1. but if var is in a function scope ...then it can be only available in that function scope |
| 20 | +// 2. if var is in global scope ...then it can be available in all the function scope and global scope |
| 21 | +// 3. if var is in a function scope ..it will become function scope variable |
| 22 | +// 4. if var is in a function scope .. you can acces it inside that function scope at any block...inside that function scope // Having it Multiple blocks |
| 23 | +// 5. Var allows you to re declare the same variable |
| 24 | + |
| 25 | +// LET USES |
| 26 | + |
| 27 | +// 1. let can only be used in a block scope |
| 28 | +// 2. if let is in a function scope ...then it can be only available in that function scope |
| 29 | +// 3. if let is in global scope ...then it can be available in all the global scope but not in function scope |
| 30 | +// 4. let will dont allow you to redeclare the same variable |
| 31 | + |
| 32 | +// let a = "hello; |
| 33 | +// let a = "bye"; // Error a is re declared |
| 34 | + |
| 35 | +// But you can have this |
| 36 | +// let a = "hello"; |
| 37 | +// a = "bye"; |
| 38 | +// console.log(a) ; // This will work |
| 39 | + |
| 40 | +// let a = 'hello'; |
| 41 | + |
| 42 | +// function box() { |
| 43 | +// // console.log(a); |
| 44 | +// console.log("Function Scope"); |
| 45 | +// } |
| 46 | + |
| 47 | +// console.log(a); |
| 48 | + |
| 49 | +// CONST USES |
| 50 | +// const c = 'hello'; |
| 51 | +// c = 'bye'; // Error c is a constant variable |
| 52 | + |
| 53 | +// 1. const is a constant variable |
| 54 | +// 2. const is a block scope variable |
| 55 | +// 3. const will not work out of the function scope |
| 56 | +// 4. const will not allow you to redeclare the same variable |
| 57 | +// 5. const will not allow you to reassign the same variable |
| 58 | +// const cc = 'cc' |
| 59 | + |
| 60 | +// function box() { |
| 61 | +// console.log('Const Scope'); |
| 62 | + |
| 63 | +// console.log(cc); |
| 64 | +// } |
| 65 | +// box(); |
| 66 | +// Output: cc |
| 67 | + |
| 68 | +// If const is declared outside .....you can still access inside the function |
| 69 | +// If const is declared inside .....you can not access outside the function |
0 commit comments