Difference Between var and let in JavaScript

Difference Between var and let in JavaScript

5 mins read56 Views Comment
Esha
Esha Gupta
Associate Senior Executive
Updated on Mar 21, 2024 17:27 IST

In JavaScript, var declares a variable with function-scope or globally if declared outside any function, allowing for potential hoisting issues and redeclarations. On the other hand, let introduces block-scoped variables, providing tighter control over the variable's lifecycle and preventing redeclarations within the same scope, leading to more predictable and safer code. Let's understand more!

 

In JavaScript, ‘var’ and ‘let’ are both used for variable declaration, but they have different behaviour and scope. ‘var‘ is a keyword used for variable declaration in JavaScript. It has been part of JavaScript since its early versions, while ‘let‘ is a newer keyword introduced in ES6 (ECMAScript 2015) for variable declaration. It provides a block-scoped alternative to ‘var‘.

If you want to be a certified JavaScript developer, then Explore Online JavaScript Courses

Table of Content

Recommended online courses

Best-suited JavaScript courses for you

Learn JavaScript with these high-rated online courses

2.05 K
3 months
2.2 K
4 weeks
– / –
40 hours
– / –
40 hours
8.5 K
62 hours
– / –
40 hours
– / –
40 hours
– / –
40 hours

Difference Between var and let in JavaScript

Below is a table showing the differences between ‘var’ and ‘let’ in Javascript

Aspect var let
ECMAScript Version Introduced in ECMAScript 1 (ES1, 1997) Introduced in ECMAScript 2015 (ES6)
Syntax var name = value; let name = value;
Scope Function scope. Block scope.
Global Object Property Becomes a property of the global object in global scope. Does not become a property of the global object in global scope
Re-Declaration in Same Scope Allowed. Can re-declare a variable. Not allowed. Syntax error if a variable is re-declared.
Hoisting Variables are hoisted; accessible as undefined before their actual declaration in the code. Variables are hoisted but not initialized. Accessing them before declaration results in a ReferenceError. Temporal Dead Zone applies.
Example var websiteName = “shikshaonline”; let courseName = “JavaScript Basics”;
Introduction to JavaScript Data Types With Examples
Introduction to JavaScript Data Types With Examples
JavaScript supports several data types including numbers, strings, booleans, arrays, objects, null, and undefined. Numbers represent numeric values, strings represent text, booleans represent true or false values, arrays store multiple...read more
What is JS Full Form?
What is JS Full Form?
JavaScript is a widely-used programming language for web development, known for its ability to create interactive and dynamic elements on websites. It plays a crucial role in modern web applications,...read more
How to Link JavaScript to HTML
How to Link JavaScript to HTML
Have you ever wondered how to make your website engaging and responsive? Linking JavaScript to HTML is the key to achieving this. Let’s understand more! Learning how to Link JavaScript...read more

What is ‘var’ in JavaScript

In JavaScript, var is a keyword used to declare variables. Variables are fundamental to any programming language, serving as placeholders or containers for values that can change over time. The var keyword was the primary way to declare variables in JavaScript before the introduction of let and const in ES6 (ECMAScript 2015).

Characteristics of ‘var

  • Function Scope: Variables declared with var are scoped to the nearest function block. If declared outside of a function, they are globally scoped.
  • Hoisting: var declarations are “hoisted” to the top of their scope. This means that the variable can be used before it’s declared, although it will be undefined until its declaration and initialization (if any) are reached in the code.
  • Re-Declaration: Variables declared with var can be re-declared within the same scope without causing an error, which can lead to bugs if done accidentally.
  • Initialization: If you declare a variable with var without initializing it (i.e., without assigning it a value), it will have the value undefined.
 

What is ‘let’ in JavaScript

In JavaScript, let is a keyword introduced in ES6 (ECMAScript 2015) that is used for variable declaration. It improves upon var by providing block-level scoping, which makes it more predictable and easier to manage in modern JavaScript development.

Characteristics of ‘let’

  • Block Scope: Unlike var, which is function-scoped, variables declared with let are block-scoped. This means they only exist within the nearest set of curly braces ({}) such as those used in loops, if-statements, or other blocks.
  • No Hoisting to Scope Start: Variables declared with let are hoisted but not initialized. This means the variable exists during the entire block but cannot be accessed until after the declaration is encountered. Accessing the variable before the declaration results in a ReferenceError.
  • No Re-Declaration in the Same Scope: With let, re-declaring the same variable within the same scope is not allowed and results in a syntax error. This helps avoid accidental re-declarations, which can lead to bugs.
  • Temporal Dead Zone: The period from the start of the block until the declaration is reached is known as the Temporal Dead Zone (TDZ) for that variable. During this time, the variable is in a “dead zone” where accessing it results in an error.

Similarities between var and let in JavaScript

Table highlighting the key similarities between ‘var’ and ‘let’ in JavaScript

Aspect var & let
Purpose Both are used for variable declaration.
Data Types Both can declare variables of any data type (number, string, object, etc.).
Re-assignable Variables declared by both can be reassigned.
Functionality Both can be used to store data that can be changed later.

Thus, the differences between var and let in JavaScript center around their scoping, hoisting behaviour, and how they are declared in relation to the global object. Keep learning, keep exploring!

FAQs

What is the Scope Difference Between var and let?

var is function-scoped, meaning a variable declared with var is accessible within the entire function it was declared in, or globally if declared outside of a function. let, however, is block-scoped, meaning it is only accessible within the block ({}) it was declared in, such as loops or conditionals, providing a more controlled and predictable variable scope.

Can var and let Variables be Redeclared?

Variables declared with var can be redeclared within the same scope without causing an error, potentially leading to bugs. However, attempting to redeclare a variable using let within the same scope will result in a syntax error, making let safer to use in scenarios where variables are tightly scoped and should not be accidentally overwritten.

How do var and let Behave with Hoisting?

Both var and let are hoisted, but with a key difference. Variables declared with var are hoisted to the top of their function or global scope and are initialized with undefined, making them accessible before their declaration line. In contrast, let declarations are also hoisted but are not initialized, leading to a "Temporal Dead Zone" from the start of the block until the declaration is reached, during which access to the variable results in a ReferenceError.

Are There Initialization Differences Between var and let?

Yes, while both var and let can be declared without initialization, a var variable will automatically be initialized with undefined, making it possible to access it before its value is explicitly set. let, however, will not initialize the variable until its declaration line is executed, preventing access to it before that point due to the Temporal Dead Zone.

When Should I Use var vs. let?

The choice between var and let depends on the needed variable scope and behavior. let is generally preferred for its block-scoping, which aligns with most other programming languages and helps avoid unintentional errors due to variable hoisting and redeclaration. var may be used in situations where function scope is explicitly desired or when maintaining legacy code that relies on function-scoped variable declarations. However, the modern consensus leans towards using let (and const for constants) for clearer, more predictable code.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio