How to Declare Variable in Javascript[3 Methods With Examples]
What is a Variable in Javascript
A variable is a container for storing data. You use variables because you can:
- you can get the information stored by the variable. In your mind, you can forget the content stored in the variable, but you can access it through Javascript.
- you can manipulate the information stored by the variable.
You can use three keywords to create variables in Javascript. These are:
- Using
var
- using
let
- Using
const
You start with the keyword(var, let, or const) and then the variable name.
keyword variablename;
Example
var username;
To assign a value to the variable, use the assignment operator(=
) followed by the data.
keyword variablename;
variablename = value;
or
keyword variablename = value;
Example
// Method 1
var username = "Liam Neeson";
// Method 2
var series;
series = "Lost";
If you call the variable username, the response on the console is Liam Neeson
.
var username = "Liam Neeson";
console.log(username);
Result
Rules of Naming Variables in Javascript
- Variable names can contain numbers, letters, and $ or _.
- Variable names cannot contain spaces or start with a number.
How to Create a Variable in Javascript
For a long time, since the creation of Javascript in 1995, the only way to declare variables was to use the var
keyword.
However, in 2015, Javascript introduced two new keywords: let
and const
. The three keywords can create variables with all types of data types.
1. How to Use var in Javascript
You can still use the var
keyword to create variables in Javascript.
var VariableName = AssignedVariableData;
Examples
// declare a variable with string data
var book = "Harry Potter"
// declare a variable with number data
var numberOfBooks = 7;
// declare a variable with boolean data
var isHeadlessNickHeadless = false;
// declare a variable with array data
var books = ["Philosopher's Stone", "Chamber of Secrets", "Prisoner of Azkaban", "Goblet of Fire"];
// declare a variable with object data
var harrypotterstatistics = {
writer: "J. K. Rowling",
maincharacters: ["Harry Potter", "Ron Wesley", "Hermione Granger"],
numberofbooks: 7,
}
// declare a variable with undefined data
var HeWhoMustNotBeNamed;
// declare a variable with null data
var MalfoyHogwartHouseBeforeSorting = null;
2. How to Use let in Javascript
You can also use the let
keyword in Javascript to create variables.
Examples
// declare a variable with string data
let fangmeaning = "Facebook, Amazon, Apple, Netflix and Google";
// declare a variable with number data
let numberoffangcompanies = 4;
// declare a variable with boolean data
let NetflixIsFangCompany = true;
// declare a variable with array data
let fangcompanies =["Meta", "Amazon", "Apple", "Netflix", "Google"];
// declare a variable with object data
let fangbrands = {
alphabet:["Google", "Youtube"],
meta:["Facebook", "Instagram"],
netflix: ["Netflix"],
apple: ["Apple"],
amazon: ["Amazon", "Alexa"]
}
// declare a variable with undefined data
let faang;
// declare a variable with null data
let faang = null;
3. How to Use const in Javascript
Finally, you can use the const
keyword to declare variables in Javascript.
Examples
// declare a variable with string data
const series = "Person of Interest";
// declare a variable with number data
const positionrottentomatoes = 12;
// declare a variable with boolean data
const stillairing = false;
// declare a variable with array data
const characters = ["Harold Finch", "Joss Carter", "John Reese", "Sameen Shaw"];
// declare a variable with object data
const series = {
name: "Person of Interest",
seasons: 5,
genres: ["Action", "Drama", "Science fiction", "Police procedural"]
}
// declare a variable with undefined data
const age;
// declare a variable with null data
const NewEpisodes = null;