JavaScript Variables

Published: (May 14, 2026 at 09:42 AM EDT)
1 min read
Source: Dev.to

Source: Dev.to

Article Image

var

  • Old way of declaring variables
  • Scope is function‑based (not limited to blocks)
  • Can be changed later
  • Can be redeclared in the same scope
  • Less safe and can lead to unexpected behavior

let

  • Modern way to declare variables
  • Scope is block‑based
  • Value can be changed (reassigned)
  • Cannot be redeclared in the same scope
  • Safer and more predictable than var

const

  • Modern way for fixed values
  • Scope is block‑based
  • Value cannot be changed or reassigned
  • Must be initialized when declared
  • Used for values that should stay constant

Scope (where variables exist)

  • var → function scope
  • let and const → block scope

Hoisting (simple idea)

  • var is available before declaration but has an empty value initially
  • let and const are also known before declaration but cannot be used before they are defined

JavaScript Modes

Sloppy Mode

  • Default mode
  • Less strict rules
  • Allows unsafe or accidental behaviors

Strict Mode

  • Activated manually
  • Prevents many common mistakes
  • Safer and recommended for real projects
0 views
Back to Blog

Related posts

Read more »

Understanding 'this' in JavaScript

Overview One JavaScript concept that confuses many developers is the behavior of this. While it may seem simple at first glance, the value of this changes depe...

Self Introduction - The beginning

Introduction After a long time hesitating, I’ve finally decided to start learning programming from scratch — and this time, I’m going all in. Like many others,...