What is the const keyword in JavaScript?

var, let, and const are probably the first words you hear about while learning JavaScript. Since the let keyword has been already explained in this Answer, and var doesn’t deserve a separate article yet, let’s find out what const is:

const

  • const is a JavaScript keyword introduced by ES6 that doesn’t have a previous equivalent.
let x = 10;

const x = 10; //This is not possible.
  • const creates block-scoped and read-only reference that cannot be reassigned.
const x = 10;
x = 15;
console.log(x); //This is will throw an error because
//x is read-only
  • It is a good practice to write const identifiers in all-uppercase (to vary them from let variables).
const TEMP_X = 10;
  • A value assigned to the const keyword has to be initialized immediately. A const keyword without an assigned value will cause a syntax error.
const X; //This is not possible.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources