— javaScript — 1 min read
Declaring a variable is a fundamental concept to writing programs in JavaScript. You can declare a variable a few different ways.
Assign values to a variable using the var
, let
, or const
keywords. Using one over the other depends on your use case.
We won’t go super deep into the functionality of each, but I'll discuss them in another post.
The first way is using the var
keyword. I'd consider this to be the classic way of declaring variables and prior to ES6 it was the only way to declare a variable.
First, you would write the var keyword then give the variable a custom name followed by the equal sign. After the equal sign you'd give the variable a value, assigning that value to that specific variable.
This value could be a string, number, function, object or even another variable and more. Below is an example of declaring a variable using the var
keyword.
1var firstName = 'DeAndre'
When using the let keyword you can use it the same way as the var keyword. There are a few differences in the things you can do with this keyword.
1let lastNmae = 'Boston'
The keyword const
uses the same syntax as var
and let
. Below is an example of using the keyword const
.
1const city = 'Chicago'
Learn more about declarations in javaScript here.