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 “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #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
Column and Table Alias in PostgresPostgresHow to add Local State to a Class in React?ReactSQL Inner JoinSQL/MySQLUnderstand Inheritance in PHP in this Basic Example (For Beginners)PHPHow to create a full screen loader using CSS and JavaScript?CSSCrypto.getRandomValues() method in JavaScriptJavaScriptWhat are Controlled Components in React?ReactHow to use $IF operator in Envision Basic?Envision BasicHow to hide and show menu on scroll using JavaScript and CSS?CSSSome Useful Operators in The SQL WHERE ClauseSQL/MySQLDifferent Types of Functions in JavaScriptJavaScriptGap in Flex?CSS