Skip to main content

Command Palette

Search for a command to run...

01. BASICS | Variables in JavaScript

This article will talk about javascript variables.

Updated
2 min readView as Markdown
S
Full-Stack Developer with 4 years' experience, specializing in backend development. Skilled in JavaScript, React, Python, Databases, and AWS. Known for building scalable web apps, leading teams, and maintaining strong client communication. Upskilling in Generative AI.

var

  • Can be re-declared and updated within its scope.
var name = "Shubham";

// ✅ Below operations are possible
name = "Amit"; // only updating
var name = "Amit"; // re-declaration and updating
  • It is not compulsory to initialize a var variable during declaration.
var name;

let

  • Can be updated but not re-declared.
let name = "Shubham";

name = "Amit"; // can be updated

// ❌ Below operation is not possible
let name = "Amit"; // re-declaration and updating
  • It is not compulsory to initialize a let variable during declaration.
let name;

const

  • Can neither be re-declared nor updated.
const name = "Shubham";

// ❌ Below operations are not possible
name = "Amit"; // only updating
const name = "Amit"; // re-declaration and updating
  • Must be initialized during declaration.
// ❌ Only declaration is not possible
const name;

Rules for choosing variable names

  1. Only letters, digits, underscore(_) and dollar($) allowed.

  2. Must begin with a letter or _ or $. Must not begin with a digit.

  3. JavaScript reserved words can't be used as a variable name.

  4. Variable names are case sensitive (name, Name and NaMe are different variable names).


Happy reading!