DEV Community

Samuel Lucas
Samuel Lucas

Posted on

Creating variables in JavaScript

Variables: what are variables? A variable can be thought of as a container that stores items.

There are two types of variables, you have one which you can reassign values to and another which you can't reassign values to.

Let's see the different variables we have with examples and how they function.

1. var
✅ how to use: var name = "Lucas"
✅ function: it is a "reassignable" variable. Meaning you can do something like this in your code
var name = 'Lucas'
var name = 'Samuel'
The result is that the variable name will hold the last assigned item, meaning it will hold Samuel.

2. let
✅ how to use: let name = "Lucas"
✅ function: it is a "reassignable" variable. Meaning you can do something like this in your code
let name = 'Lucas'
let name = 'Samuel'
The result is that the variable name will hold the last assigned item, meaning it will hold Samuel.

3. const
✅ how to use: const name = "Lucas"
✅ function: it is a "non reassignable" variable. Meaning you cannot do something like this in your code
const name = 'Lucas'
const name = 'Samuel'
An error will occur that you cannot reassign values it items to name.

There's a catch to var tho' and that's the fact that it's no longer in use from the previous es6.

Why? I don't know as well 🤷, just kidding. Read it up from here to learn more. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

Top comments (0)