Let vs Var vs Const

JAVASCRIPT is the new Boss in the Industry. Javascript says nothing is impossible.
Javascript is having many frameworks like Vue, Angular and libraries like React for frontend Development and at the same time it can even tackle Backend Development usin…


This content originally appeared on DEV Community and was authored by Sankalp Swami

JAVASCRIPT is the new Boss in the Industry. Javascript says nothing is impossible.
Javascript is having many frameworks like Vue, Angular and libraries like React for frontend Development and at the same time it can even tackle Backend Development using Node JS. And recently there is also a new buzz for Machine Learning with Javascript.
Watching someone writing Javascript is as soothing as watching these two -
chandler & joe hug

LET

Let statement is block-scoped and not a Global scope local variable used for initializing a statement. This statement can be used in function as it is function-scoped and one of the main character of let statement is that it can be reassigned.

let name = "Sankalp";
name = "Sankee";
console.log(name); // will print: Sankee

VAR

Var statement is also used for variable declaration and it is Global scoped variable. Variable declared with var is defined throughout the program.Var is defined throughout the function but not if its declared in a block as it is not block-scoped.

if (true) {
var firstName = 'Sankalp'
let age = 18
}
console.log(firstName) // Will print: Sankalp
console.log(age) // Will print: ReferenceError

Var statement can be reassigned.

var name = 'Sankalp'
name = 'sankee'
console.log(name) // will print: sankee

CONST

When we create a variable with let, we can reassign its value later on. Thats great but what if we dont want to reassign its value, for those, we have const. Const is function-scoped and block-scoped and it is not defined throughout the program as it is not global-scoped. If you try to reassign const variable it will throw a type error.

const name = 'Sankalp';
name = 'Sankee'
console.log(name) // will print: TypeError

Hoisting

Variables declared with var can be accessed even before they are declared. The reason for this behaviour is that they are hoisted up to the top of the scope. The above code runs as if it was the code below.

name = "sankee"
console.log(name) // Will print: sankee
var name

Final Verdict

Var variable is something I will not recommend to use as it is not used in the Industry. Let and Const are the one which you will be using. choosing them is easy as if you want to reassign its value use let and if you are then got for const variable.

Getting strong with the fundamentals is the only path to Glory.
Wish you very all the Best, Keep Practicing, Peace..
Joey & Chandler


This content originally appeared on DEV Community and was authored by Sankalp Swami


Print Share Comment Cite Upload Translate Updates
APA

Sankalp Swami | Sciencx (2021-08-01T08:31:07+00:00) Let vs Var vs Const. Retrieved from https://www.scien.cx/2021/08/01/let-vs-var-vs-const/

MLA
" » Let vs Var vs Const." Sankalp Swami | Sciencx - Sunday August 1, 2021, https://www.scien.cx/2021/08/01/let-vs-var-vs-const/
HARVARD
Sankalp Swami | Sciencx Sunday August 1, 2021 » Let vs Var vs Const., viewed ,<https://www.scien.cx/2021/08/01/let-vs-var-vs-const/>
VANCOUVER
Sankalp Swami | Sciencx - » Let vs Var vs Const. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/01/let-vs-var-vs-const/
CHICAGO
" » Let vs Var vs Const." Sankalp Swami | Sciencx - Accessed . https://www.scien.cx/2021/08/01/let-vs-var-vs-const/
IEEE
" » Let vs Var vs Const." Sankalp Swami | Sciencx [Online]. Available: https://www.scien.cx/2021/08/01/let-vs-var-vs-const/. [Accessed: ]
rf:citation
» Let vs Var vs Const | Sankalp Swami | Sciencx | https://www.scien.cx/2021/08/01/let-vs-var-vs-const/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.