Yogesh Chauhan's Blog

The simple difference between var, let and const in Javascript

in JavaScript on February 22, 2020

var

A var variable can be re-declared and updated. var is “function scoped”.

The scope of a variable defined with “var” is limited to that “function”. If it is defined outside any function, then the scope of the variable is global.

let

A let variable be be updated but not re-declared. “let” is “block scoped”.

The scope of a variable defined with “let” is limited to the “block” which is defined by curly braces -> {}

const

As per the name suggests, const variables cannot be updated. 

We can not re-assign value to the variable but it CAN be mutated.

Same as "Let", The scope of a variable defined with “let” is limited to the “block” which is defined by curly braces -> {


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How does @extend rule work in SCSS (Sass)?SCSSHow to use a Subquery to Insert Multiple Rows in SQL Table?SQL/MySQLAvoid using new Array() in JavaScriptJavaScript5 Steps to Create a Line using Canvas Tag in HTML5HTMLWhy does MOV matter in IT or any kind of projects?MiscellaneousWhat’s the difference between a Framework and a Library?MiscellaneousHow to Create a Copy of a Table in SQL and MySQL?SQL/MySQLEverything you need to know about ANY, SOME and ALL Operators in PostgresPostgresAngular 9 time clock update every minute, second, hourAngularHow to zoom an element on hover using CSS?CSSHow to Commit and Rollback Changes in SQL?SQL/MySQLHow to verify your domain in Google Console with a TXT record?Miscellaneous