I am Lakshmi. I've completed my graduation in the stream of ECE. I love reading and like to listen more than I speak up.
Global Variables and Local variables
- The variable declared without keyword is categorized under a global variable irrespective of its place of declaration.
- The variable declared outside of all the functions has global scope(it is accessible throughout the program without passing as a parameter into the function), and it can use the var keyword.
- Local variables have a lifespan within a function and block.
Var, Let, and Const
- As we have seen earlier, A variable declared with a var keyword has a global scope when it is declared outside the function.
- It is limited to the function scope when declared inside the function. but, if it is declared within a block, we can access it outside the block.
- It can be re-declared and updated within its scope.
- The variable declared with a let keyword has a global scope when it is declared outside the function.
- A variable declared with a let keyword can be accessible within a block, where block defines the group of conditional statements within the curly braces(if,if-else, for, while,etc.,
- It cannot be re-declared, if we try to re-declare the variable, we will get a syntax error.
- We can update the variable, but the issue is the scope of the variable will change to global.
- A variable declared with const keyword is constant forever, if it is declared outside the function it will have global scope. When it is declared inside a function, it will have local scope.
- It cannot be re-declared within its scope, if we try to re-declare the variable, we will get a syntax error.
- We cannot update or re-assign the value of const, doing so will produce TypeError.
This content is accurate and true to the best of the author’s knowledge and is not meant to substitute for formal and individualized advice from a qualified professional.
© 2020 Lakshmi