Variables are one of the main fundamentals of programming. They are used to store data or allocate memory that can later be used to create or do bigger tasks. Such tasks can include but are not limited to: simple math, unlocking doors, etc. Within JavaScript, there are 4 ways to create working variables. They are known as let, const, var, and none.
w3schools.com
mozilla.org
replit
stackoverflow
=
Before we get into the variable types, we must learn and understand how to set variables. To do this, we use the = operator. Remember to use only one =. If we were to use two = operators, what would be happening is that rather than assigning/declaring a variable, you would be comparing the two values instead, which then returns a boolean, or a true/false statement. When using three = operators, not only are you comparing the values, but you are also comparing the data types. Again, you would be returned with a boolean of either true/false.
'8' == 8
//returns true when log'd
8 === 8
// returns true when log'd
8 === 9
// returns false when log'd
let
let is the most commonly used keyword to declare variables with due to its simplicity and ability to be moldable/changeable. This allows the user to alter the value of any variable that has been set with a let. Remember that variables using the let keyword can only be accessible when within the right scope.
let a = 5 //assigning variable a to store the value of 5
// returns 5 when log'd
let b = 8 //assigning variable b to store the value of 8
// returns 8 when log'd
b = b + a //change b so that b is equal to b(8) + a(5)
// returns 13 when log'd
const
const is the second most used keyword when it comes to declaring variables. Unlike the keyword let, const cannot be changed or altered in anyway. Once you set it to an initial value, it cannot be become another value. Although it cannot be mutable like let, it also can only be accessed when within the correct scope, just like let.
const a = 5 //assigning variable a to store the value of 5
// returns 5 when log'd
a + 3 //make a(5) add 3
// returns 5 when log'd
var
var used to be the only keyword used to declare variables, after an update in 2015 which brought let and const into JavaScript. If used in present day JavaScript, it can lead to bugs and errors so it is best to only use it with old code. Like let, var can also be altered later on in the code you make. var can also be used globally in your code. This means the you can access the variable no matter which scope you are in.
var a = 5//assigning variable a to store the value of 5
// returns 5 when log'd
a + 3 //make a(5) add 3
// returns 8 when log'd
None
Yes, that's right, nothing. Although you can create variables, it is better to declare and assign variables using the previous keywords due to it's common practice and their common usage.
a = 5
// when log'd, a will produce 5
b = 3
// when log'd b will produce 3
There you have it, a small rundown on declaring variables in JavaScript. If you're still wondering which keyword to use or how you should be declaring variables, almost always use let. Altering variables within code is common, thus const is not always the best option due to it's ability to not be changeable. Remember to always declare your variables.
Top comments (0)