var vs let in JavaScript W3Schools: Complete Guide

When delving into JavaScript, you'll encounter two primary ways to declare variables: `var` and `let`. Both have unique characteristics and use cases, so understanding their differences is pivotal in writing efficient and maintainable code. This article, inspired by w3schools, will guide you through these two declarees, their differences, and when to use each.

JavaScript Variables Var | Let | Const | in Urdu / Hindi | lecture 6
JavaScript Variables Var | Let | Const | in Urdu / Hindi | lecture 6

Let's kickstart our exploration by discussing the basics of `var` and `let`.

Javascript
Javascript

Understanding `var`

The `var` keyword was traditionally used in JavaScript for variable declaration. It has been around since the early days of JavaScript and is widely used in legacy code.

JavaScript var vs let vs const: The Complete Guide for Beginners (2026)
JavaScript var vs let vs const: The Complete Guide for Beginners (2026)

However, the global scope and function scope of `var` can lead to unexpected behavior, especially when you want to create block-scoped variables. This is where `let` comes in.

Variable Hoisting with `var`

var, let and const keyword difference
var, let and const keyword difference

One of the peculiar aspects of `var` is its hoisting behavior. JavaScript hoists `var` declarations to the top of their containing function or globally, initializing them with `undefined`. This can lead to unexpected results if not handled properly. For example:

```javascript console.log(x); // Outputs: undefined var x = 5; ```

`x` is considered declared but initially assigned the value `undefined`.

Function-Scoped vs Global Scope

JavaScript Variables: The Difference Between Var, Let, and Const
JavaScript Variables: The Difference Between Var, Let, and Const

When declared with `var`, variables are either function-scoped or global-scoped. This can make it challenging to manage complex applications with hundreds of functions and lines of code. For instance:

```javascript var y = 5; function test() { var y = 10; console.log(y); // Outputs: 10 } test(); console.log(y); // Outputs: 5 ```

Even though `y` is declared with `var` inside the function, it's still in the same global scope as the `y` declared outside the function.

The Rise of `let`

A little reminder of how and when to use the right data container!
A little reminder of how and when to use the right data container!

Introduced in ECMAScript 6, `let` offers block scope, which makes it easier to manage and reason about variables in your JavaScript code.

let is perfect for creating variables that should only exist within a specific block of code, such as an `if` statement, a loop, or a function expression.

JS variables
JS variables
the different types of computers are shown in this diagram, which shows how to use them
the different types of computers are shown in this diagram, which shows how to use them
TypeScript vs JavaScript: Which Is Best in 2022
TypeScript vs JavaScript: Which Is Best in 2022
an old computer screen with the word var let on it and a yellow arrow above it
an old computer screen with the word var let on it and a yellow arrow above it
Master JavaScript Variables: A Beginner’s Guide 🚀
Master JavaScript Variables: A Beginner’s Guide 🚀
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
the different types of computers are shown in this diagram, and each one has its own language
the different types of computers are shown in this diagram, and each one has its own language
a diagram showing the different types of data
a diagram showing the different types of data
a diagram that shows the different types of web pages and what they are used for
a diagram that shows the different types of web pages and what they are used for
two pictures with the same caption in different languages, one has an image of a man
two pictures with the same caption in different languages, one has an image of a man
Variables in JavaScript Explained Simply
Variables in JavaScript Explained Simply
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom
JavaScript Return Values: Get the Most Out of Your Functions 🚀
JavaScript Return Values: Get the Most Out of Your Functions 🚀
TYPESCRIPT VS JAVASCRIPT (2026)
TYPESCRIPT VS JAVASCRIPT (2026)
JavaScript Variables
JavaScript Variables
Java vs JavaScript: Here’s What You Need To Know | HackerNoon
Java vs JavaScript: Here’s What You Need To Know | HackerNoon
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳
🚀 Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! 💻✨
🚀 Master JavaScript Loops: For, While, Do-While, For-In & For-Of Loops Explained! 💻✨
8 Essential JavaScript Array Methods Visual Guide
8 Essential JavaScript Array Methods Visual Guide
Javascript Cheet cheet
Javascript Cheet cheet

Block Scoping with `let`

Variables declared with `let` are function-scoped or block-scoped. This means they're only accessible within the block, switch, or expression they're declared in. Here's an example:

```javascript let a = 5; if (true) { let a = 10; console.log(a); // Outputs: 10 } console.log(a); // Outputs: 5 ```

Unlike `var`, the `let` declared inside the `if` statement doesn't override the `let` declared outside.

No Hoisting with `let`

`let` keywords are not hoisted to the top of their scope. Instead, they're initially assigned the value `undefined`. Attempting to access a `let` variable before its declaration results in a ReferenceError. Here's an example:

```javascript console.log(b); // Throws a ReferenceError let b = 5; ```

This behavior encourages more predictable and safer coding practices.

In conclusion, understanding the differences between `var` and `let` is crucial for writing clean, efficient, and maintainable JavaScript code. While `var` has its place in legacy code and some specific use cases, `let` is generally recommended for its superior block scoping and more intuitive behavior. Experiment with both in your projects, and you'll soon find your JavaScript flows more smoothly than ever.

Related Articles

Upload Image To Website Javascript W3schools Javascript Formdata While Loop Javascript Syntax Var Let Const Javascript W3schools Javascript Array With Unique Objects W3schools Javascript Input Validation Javascript Get Url Parameter W3schools Data Validation In Javascript W3schools Display Javascript Output In Html W3schools Javascript Form Events