GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise
  • function-scoped
  • hoisting

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — JavaScript Variables - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • JS Tutorial
  • Web Tutorial
  • A to Z Guide
  • Projects
  • OOP
  • DOM
  • Set
  • Map
  • Math
  • Number
  • Boolean
  • Exercise

JavaScript Variables

Last Updated : 15 Jun, 2026

Variables in JavaScript are used to store data values. They can be declared in different ways depending on how the value should behave.

  • Variables can be declared using var, let, or const.
  • JavaScript is dynamically typed, so types are decided at runtime.
  • You don’t need to specify a data type when creating a variable.
JavaScript
// Old style
var a = 10    

// Prferred for non-const
let b = 20;    

// Preferred for const (cannot be changed)
const c = 30;  

console.log(a);
console.log(b);
console.log(c);

Declaring Variables in JavaScript

  • Before ES6 (2015): Variables were declared only with var, which is function-scoped and global-scoped, causing issues like hoisting and global pollution.
  • ES6 Introduction:let and const were introduced to provide safer alternatives for declaring variables.
  • Scope: let and const are block-scoped (limited to { } block) or global-scoped, reducing errors compared to var.

1. var keyword

var is a keyword in JavaScript used to declare variables and it is Function-scoped and hoisted, allowing redeclaration but can lead to unexpected bugs.

JavaScript
var a = "Hello Geeks";
var b = 10;
console.log(a);
console.log(b);

2. let keyword

let is a keyword in JavaScript used to declare variables and it is Block-scoped and not hoisted to the top, suitable for mutable variables

JavaScript
let a = 12
let b = "gfg";
console.log(a);
console.log(b);

3. const keyword

const is a keyword in JavaScript used to declare variables and it is Block-scoped, immutable bindings that can't be reassigned, though objects can still be mutated.

JavaScript
const a = 5
let b = "gfg";
console.log(a);
console.log(b);

Rules for Naming Variables

When naming variables in JavaScript, follow these rules

  • Variable names must begin with a letter, underscore (_), or dollar sign ($).
  • Subsequent characters can be letters, numbers, underscores, or dollar signs.
  • Variable names are case-sensitive (e.g., age and Age are different variables).
  • Reserved keywords (like function, class, return, etc.) cannot be used as variable names.
JavaScript
let userName = "Suman";  // Valid
let $price = 100;         // Valid
let _temp = 0;            // Valid
let 123name = "Ajay";    // Invalid
let function = "gfg"; // Invalid

Interesting Facts about Variables in JavaScript

1. let or const are preferred over var: Initially, all the variables in JavaScript were written using the var keyword but in ES6 the keywords let and const were introduced. The main issue with var is, scoping.

JavaScript
if (true) {
    var x = 10;
    let y = 20;
}

console.log(x);  // 10 (var is function-scoped)
console.log(y);  // Error (let is block-scoped)

2. var is function scoped: Can be accessed outside block if within the function.

JavaScript
if (true) {
  var x = 10; 
}
// Accessible outside the block
// because we are in same function
console.log(x);

3. let and const are block scoped : Cannot be accessed outside block even if inside the same function

JavaScript
if (true) {
  let y = 20;
  const z = 30;
}
console.log(y, z); // ReferenceError

4. var can be redeclared in the same scope, but let and const cannot be

JavaScript
var x = 10;
var x = 20; // Allowed

let y = 30;
let y = 40; // SyntaxError

const z = 50;
const z = 60; // SyntaxError

5. We can change elements of array or objects even if declared as const.

JavaScript
const ob = { a: 10 };
ob.a = 20; // Allowed

const arr = [10, 20, 30]
arr[2] = 40
console.log(arr)  // Allowed

/* TypeError in the below lines
obj = { b: 30 }; 
arr = [50, 100] */

Also Check:

  • Variable Scope in JavaScript
  • Variable Shadowing in JavaScript
  • var Vs let Vs const
Comment

Explore

    JavaScript Basics
    • Introduction3 min read
    • Variables5 min read
    • Operators4 min read
    • Control Statements4 min read
    Array & String
    • Arrays6 min read
    • Array Methods4 min read
    • String5 min read
    • String Methods8 min read
    Function & Object
    • Functions4 min read
    • Function Expression3 min read
    • Function Overloading4 min read
    • Objects4 min read
    • Constructors4 min read
    OOP
    • OOP5 min read
    • Classes & Objects4 min read
    • Access Modifiers5 min read
    • Constructor4 min read
    Asynchronous JavaScript
    • Asynchronous2 min read
    • Callbacks4 min read
    • Promises4 min read
    • Event Loop3 min read
    • Async Await Fuction3 min read
    Exception Handling
    • Error and Exceptional Handling5 min read
    • Errors Throw & Try to Catch2 min read
    • Custom Errors2 min read
    • TypeError Invalid Array.prototype.sort argument1 min read
    DOM
    • DOM8 min read
    • DOM Elements3 min read
    • Custom Events4 min read
    • Addeventlistener5 min read
    Advanced Topics
    • Closure3 min read
    • Hoisting5 min read
    • Scope4 min read
    • Higher Order Functions7 min read
    • Debugging3 min read
    Courses
    • Placement 360 Course2 min read
    • MERN Full Stack Course2 min read
    • DSA and System Design Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.