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

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
4 different ways to create JavaScript ObjectsJavaScriptWhat’s Interpolation in SCSS (Sass)?SCSSHow to set opacity or transparency using CSS?CSSSome interesting HTML Input Attributes to rememberHTMLHow to Install PHP composer in Mac OS Catalina?PHPCheck if any column has NULL values in PostgresPostgresHow to create a Recent Posts function in WordPress?WordPressConfiguring Modules with @forward rule in SCSS (Sass)SCSSHow to add Lifecycle Methods to a Class in React?ReactIs monitoring an employee’s work on a computer a desirable or undesirable activity?MiscellaneousCasting in PostgreSQLPostgresOverview of Drupal and how it worksDrupal