Variable Differences in JavaScript

let greeting;
var greeting;
let greeting = "Hello, World";
var greeting = "Hello, World";
const greeting = "Hello, World";
  1. let greeting
  • Can declare a variable without assignment or initialization
  • let greeting;
  • Block-scoped meaning the ability to also declare a variable in any set of curly brackets. However, be aware of your scope when assigning variables in let. Your value is contingent on the scope.
let greeting = "Hello, World";   if(true) {
let greeting = "Bye"
console.log(greeting) //=> "Bye
console.log(greeting) //=> "Hello, World";
  • Can be reassigned but not redeclared within scope
let greeting = "Hello, World"
greeting = "Bye, World"
let greeting = "Bye, World" // => ERROR
  • Can’t be redeclared or reassigned, always pointing to the same object. That being said, if you try to assign a const variable without a value, you cannot reassign it with a value
const greeting;
greeting = "Hello, World" //=> ERROR
  • Variable is initialized during declaration so the correct way to declare and assign is:
const greeting = "Hello, World"
  • BLOCK -SCOPED like let, meaning that they can be accessed within the scope that they were initialized
  • Can declare variable without assigning value upon initialization
var greeting;
  • Can be redeclared AND reassigned
  • function and globally scoped, meaning that we can we can redeclare and reassign within the same scope. YIKES! How convenient but you can also anticipate issues further down the road.
var greeting = "Hello, World"
var greeting = "Bye, World"
  • let and const are block scoped variables
  • const cannot be redeclared or reassigned after initialized at declaration
  • var is function and globally scoped, that can be redeclared and reassigned, which can cause scope issues! Avoid var




Fullstack Software Engineer | Musician | Food Lover | Coffee Addict

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The journey to become a full-stack JavaScript developer starts here.

The Unexpected Side Effect of Selections

How to use React Testing Library to rewrite an Enzyme Component test

Building an Android app (Episode #2): with Ionic

Understand modern Redux — When will immer.js create a new reference?

D3.js Tutorial: Build Your First Bar Chart

RxJS: Retry With Delay — You’ll Want to Build This Operator

Creating your first VS Code Extension

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Grace Yuiko Nakano

Grace Yuiko Nakano

Fullstack Software Engineer | Musician | Food Lover | Coffee Addict

More from Medium

Javascript SPA for Flatiron

Local Storage and Session Storage (JavaScript)

Don’t hire JavaScript developers but TypeScript

What is JavaScript ES6?