You’re just getting started with JavaScript, and you want to learn the difference between declaring a constant or variable and assigning (or initializing) it.
To declare a constant or variable is to give it an identifier so that you can reference it. To assign a value to a variable is to store information in it.
This beginner’s guide will teach you how to declare constants and variables, how to assign them values, and more.
To Declare
In JavaScript, you can create constants and variables—basically, objects that hold information—and give them values that you can use later in your code.
You create a constant or variable by declaring it with a const
, let
, or var
statement.
Declare a Constant
The const
statement is for constants:
// Declare a constant
const firstName;
Constants cannot be redeclared or reassigned.
Declare a Variable With Let
The let
statement is for variables with a block scope:
// Declare a variable with the "let" statement
let firstName;
Variables declared with the let
statement cannot be redeclared but can be reassigned.
Declare a Variable With Var
And the var statement is for variable with a function scope or global scope:
// Declare a variable with the "var" statement
var firstName;
Variables declared with the var
statement can be redeclared and reassigned.
To Assign
Once you’ve declared a constant or variable, you can assign it a value with the assignment operator (=).
The first time you assign a value to a constant or variable is called “assignment” or “initialization.”
You can initialize a constant or variable at declaration:
// Declare "firstName" constant and assign it a value of "Jim"
const firstName = "Jim";
You can also declare your constant or variable empty, then initialize it post-declaration:
// Declare "firstName" constant
const firstName;
// Assign it a value of "Jim"
firstName = "Jim";
The Difference Between Declaring and Assigning
To declare a constant or a variable is to create a data object and give a name, so that you can reference it later in your code. To assign a constant or variable, on the other hand, is to give it a value.
Another name for declaration, if oversimplified, could be “naming it.” And another name for assignment could be “storing information in it.”
To Redeclare
In JavaScript, certain types of data objects can be declared more than once, or “redeclared.”
Constants cannot be redeclared within the same scope:
const firstName = "Jim";
console.log(firstName); // Jim
// Uncaught SyntaxError: Identifier 'firstName' has already been declared
const firstName = "John";
Neither can variables declared with the let
statement:
let firstName = "Jim";
console.log(firstName); // Jim
// Uncaught SyntaxError: Identifier 'firstName' has already been declared
let firstName = "John";
However, variables declared with the var
statement can be redeclared:
var firstName = "Jim";
console.log(firstName); // Jim
var firstName = "John";
console.log(firstName); // John
To Reassign
In JavaScript, certain types of data objects can be assigned values more than once, or “reassigned.”
Constants cannot be reassigned within the same scope:
const firstName = "Jim";
console.log(firstName); // Jim
// Uncaught TypeError: Assignment to constant variable.
firstName = "John";
Variables declared with let
or var
, on the other hand, can be reassigned within the same scope:
let firstName = "Jim";
console.log(firstName); // Jim
// Uncaught TypeError: Assignment to constant variable.
firstName = "John";
console.log(firstName); // John
Whether it is a good idea to redeclare and reassign in your code is the subject of heated debate in the JavaScript development community.
Summing It Up
const | let | var | |
---|---|---|---|
Redeclare | Not possible | Not possible | Possible |
Reassign | Not possible within the same scope | Possible | Possible |
Thank you for reading this far and I hope this tutorial helped.
You now know the difference between declaring a constant or variable and assigning it a value. You also know when you can—and cannot—redeclare and reassign constants or variables depending on the type of statement that you used.
If you have any questions, be sure to leave a reply below.