Variables

Variables are used whenever there’s a need to store a piece of data. A variable contains data that can be used in the program elsewhere. Using variables also ensures code re-usability since it can be used to replace the same value in multiple places.

const currency = '$';
let userIncome = 85000;
console.log(currency + userIncome + ' is more than the average income.');
// Output: $85000 is more than the average income.

Declaring a Variable

To declare a variable in JavaScript, any of these three keywords can be used along with a variable name:

  • var is used in pre-ES6 versions of JavaScript. It is function scoped.
  • let is the preferred way to declare a variable when it can be reassigned. It is block scoped.
  • const is the preferred way to declare a variable with a constant value. It is also block scoped.
var age;
let weight;
const numberOfFingers = 20;
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback.

Learn JavaScript on Codecademy

Edit this page on GitHub

Contributors