var, let, const and more!
In three ways we can declare variables in JavaScript. They are — var, let, and const.
Var: Var is a globally scoped variable. It means the value assigned in var can be accessed from anywhere in the program. Var can be accessed even before initializing though it will return that value as undefined. Cause that time it will be in the creation phase of execution context in JavaScript.
Now, what is execution context? When we create a JavaScript file there is formed an execution context. Everything in JavaScript happens inside of a window, and when we create a program this window is initialized in this keyword. When we execute a JavaScript program there two phases are created.
- Creation phase
- Execution phase
Creation phase: In the creation phase, for all variables and functions memories are allocated. All variables initialize as undefined.
Execution Phase: In the execution phase, all the variables who initialized as undefined are assigned values. When a function is invoked in the execution phase there is created a block scope. And inside this block scope, another execution is created.
Let: Let is a block-scoped variable. In many ways, blocks are created in Javascript. Like — loops(for, while), conditions (is, else, etc.), functions, etc. Inside these blocks, they create their own scope environment.
If the variable is initialized by let inside these scopes, the variable can be accessed only inside the block.
Also, let cannot be accessed before initializing.
Const: Const means constant which means it’s can’t be changed. Const variables are meant to be not changed, but in JavaScript, it is quite different. In JavaScript, values can’t be changed if they are primitive. There are two types of data types in JavaScript.
- Primitive — Int, float, string, number, boolean, undefined, etc.
- Non Primitive — object, array, function.
Const is also a block-scoped variable like let.
Lastly, a little discussion of how the compiler understands that this is a variable. Compilers can identify variables in many ways like — lexing, parsing, AST(Abstract Syntax Tree), etc
var greet = "Hello";
For this line, JavaScript makes an abstraction like a tree diagram.
Also another important note- In JavaScript semicolon(;) means the end of the statement. It is read and used by the compiler to distinguish between separate statements so that statements do not leak into other parts of the code. Though it’s not necessary to use semicolons in JavaScript. This is because JavaScript is clever and it can add semicolons where needed automatically. This happens behind the scenes. But we should use it cause if we don’t add semicolons in the program compiler adds itself and it is important to understand that these are not correct 100% of the time. The semicolon is used to separate statements in JavaScript — not to terminate them. Also, in some situations, you simply have to use a semicolon. Otherwise, the code will not work.
It is entirely up to you whether or not to use semicolons. In my perspective, utilizing semicolons has more advantages than leaving them off.
Thank you for sticking with me this far.
I hope you find it helpful.
Happy reading and fun coding!