loader

Loading

CoderrShyam logo

CoderrShyam

HomeAboutContactBlogTutorials
LoginSignup

Introduction

Quick StartWhat is JavaScriptHistory of JavaScript

Setup

Node.js InstallationVisual Studio Code

Basics

Syntax & Comments
Variables
Variable Naming RulesPrimitives and ObjectsOperators and Expressionsvar vs let vs const
Functions

Advanced

CoderrShyam logo

CoderrShyam

HomeAboutContactBlogTutorials
LoginSignup
TutorialsJavaScript TutorialVariablesvar vs let vs const

var vs let vs const

In JavaScript, there are three ways to declare variables:- var, let, and const. Understanding the differences between these three methods is important for writing clean and maintainable code.

var

In JavaScript, the var keyword is used to declare variables. Variables declared with var are function-scoped, which means that they are only accessible within the function in which they are declared. For example:

main.js
function myFunction() {
  var x = 10;
  console.log(x); // 10
}

the var keyword has some limitations and has been largely replaced by the let and const keywords in modern JavaScript.

One of the main issues with var is that it is function-scoped, rather than block-scoped. This means that variables declared with var are accessible within the entire function in which they are declared, rather than just within the block of code in which they appear. This can lead to unexpected behavior and can make it difficult to reason about the scope of variables in your code.

let

The let keyword was introduced in ES6 as a replacement for var. Variables declared with let are block-scoped, which means that they are only accessible within the block of code in which they are declared. For example:

main.js
if (true) {
  let x = 10;
  console.log(x); // 10
}

console.log(x); // ReferenceError: x is not defined

The let keyword is more flexible than var and is generally preferred for declaring variables in modern JavaScript code. It allows you to create variables that are scoped to a specific block of code, rather than to an entire function, which can help to prevent bugs and make your code easier to understand.

const

The const keyword is used to declare constants in JavaScript. Variables declared with const are block-scoped, like variables declared with let, but their values cannot be changed once they have been assigned. For example:

main.js
const PI = 3.14;
PI = 3.14159; // TypeError: Assignment to constant variable.

Constants are useful for declaring values that should not be changed throughout the execution of a program, such as mathematical constants or configuration settings. By using const to declare these values, you can prevent accidental changes to them and make your code more robust and maintainable.

Conclusion

  • var is function-scoped and has been largely replaced by let and const in modern JavaScript code.
  • let is block-scoped and is generally preferred for declaring variables in modern JavaScript code.
  • const is used to declare constants and is block-scoped like let, but its values cannot be changed once they have been assigned.

How is this guide?

February 26th, 2026

Operators and Expressions

In JavaScript, operators are used to perform operations on variables and values. Expressions are combinations of variables, values, and operators that are evaluated to produce a result.

If-Else Conditionals

The "if" statement in JavaScript is used to execute a block of code if a certain condition is met. The "else" clause is used to execute a block of code if the condition is not met.

© 2026CoderrShyamAll Rights Reserved.

On this page

var
let
const
Conclusion
Follow us on GitHub