|
| 1 | +// We can differentiate var, let and const keywords as follows--> |
| 2 | + |
| 3 | +/* |
| 4 | +Short Overview----> |
| 5 | +1. Scope-> var doesn't have block scope it is globally scoped where 'let' and 'const' have block scope |
| 6 | +2. Re_Declare-> var can be redeclare where let and const can't be redeclare. |
| 7 | +3. Re_Initialization-> var and let can be redeclare unlike const. |
| 8 | +4. for const variables we have to initialize it at the time of declaration unlike var and let. |
| 9 | +*/ |
| 10 | + |
| 11 | +//-------------------------- Practical Overview ---------------------------------------- |
| 12 | + |
| 13 | +// ------------------------------------------------------------- |
| 14 | +// 1. on the basis of "SCOPE"--> |
| 15 | +// var doesn't have block scope it is globally scoped where 'let' and 'const' have block scope |
| 16 | +// for example----> |
| 17 | + |
| 18 | +{ |
| 19 | + var fullName = 'Sandhya Dwivedi'; |
| 20 | + let DOB = '23/07/1999'; |
| 21 | + const age = 24.5; |
| 22 | +} |
| 23 | +// if we want to access these values outside the block output will be--> |
| 24 | +// console.log(fullName) // Sandhya Dwivedi globaly scoped |
| 25 | +// console.log(DOB) // DOB is not defined due to block scope |
| 26 | +// console.log(age) // age is not defined due to block scope |
| 27 | +// *uncomment all consol.logs to see output |
| 28 | + |
| 29 | +// ------------------------------------------------------------- |
| 30 | + |
| 31 | +// 2. on the basis of "REDECLARATION"--> |
| 32 | +// var can be redeclare where let and const can't be redeclare. |
| 33 | +// for Example var can be redeclare--> |
| 34 | +var fullName = 'Sandhya Dwivedi'; |
| 35 | +var fullName = 'Sandhya Dwivedi'; |
| 36 | +console.log(fullName); // last output--> Sandhya Dwivedi |
| 37 | +/* |
| 38 | +let DOB = '23/07/1999' |
| 39 | +let DOB = '23/07/1999' |
| 40 | +const age = 24.5; |
| 41 | +const age = 24.5; |
| 42 | +*/ |
| 43 | +// We can't do this because let and const can't be redeclare it will throw an error |
| 44 | +// *uncomment krke dekh sakte ho |
| 45 | + |
| 46 | +// ------------------------------------------------------------- |
| 47 | + |
| 48 | +// 3. on the basis of "RE_INITIALIZATION"--> |
| 49 | +// var and let can be re-initialized unlike const-- |
| 50 | + |
| 51 | +var fullName = 'Sandhya Dwivedi'; |
| 52 | +fullName = 'sandhya.dwivedi.5454'; |
| 53 | +console.log(fullName) // output--> sandhya.dwivedi.5454 |
| 54 | + |
| 55 | +let DOB = '23/07/1999'; |
| 56 | +DOB = '23/07/2001'; |
| 57 | +console.log(DOB) // output--> 23/07/2001 |
| 58 | + |
| 59 | +const age = 24.5; |
| 60 | +age = 24.6; |
| 61 | +console.log(age) // output --> throw an error - can't reassign values to const variables |
| 62 | + |
| 63 | +// ------------------------------------------------------------- |
| 64 | + |
| 65 | +// 4. on the basis of "INITIALIZATION while Declaring variables"--> |
| 66 | +// for const variables we have to initialize it at the time of declaration unlike var and let |
| 67 | +// for Example--> |
| 68 | + |
| 69 | +// const myName; // we can't do this it will throw an error |
| 70 | +const myName = 'Surendra Jatav' |
| 71 | + |
| 72 | +let name; |
| 73 | +var name1; |
| 74 | + |
0 commit comments