DEV Community

eli-shi
eli-shi

Posted on

Variables (JavaScript)

If you want to write any sort of program, you are bound to use at least one variable.
Variables are values that we want to create, calculate, or change in our program. In javascript, variable types are not distinguished by the contents(strings, array, characters, boolean, etc.), instead they are distinguished by their characteristics such as reassignability(changability) and visibility. This makes programming simpler in some cases; since different variables serve different purposes in the program, we have different types of variables which help to serve this purpose effectively.
Before we start talking about different types of variables in JavaScript, you should know what a scope is. Scope is the context of your code, which determines the visibility of variables. There are two types of scope: local and global. While variables that have global scope are visibile in all parts of your code meaning they can be used anywhere in your program, while local scoped variables are only visible within the block(function) it is declared in.
The most basic type of variable is declared(created) by typing var, this variable type has global scope and can be reassigned.
Then we have const, which is a variable type that can never be reassinged during the program and has global scope.
Another variable type is let, which can't be reassigned and has local scope. For example, if I declare a let varaible within an if-then operator, then I will not be able to use the let variable outside of that if-then operator.
Many programmers stick to using const variables in their programs since having variables be const prevents them from being accidently changed during a program, this is especially useful when a program is doing various calculations with numerous variables. If there is ever a need to change it they create a new variable referenceing the const and do the rest of the program on the new variable, so that the orignial value is still preserved if there is every a need for it.
Good luck. May the force be with you!

Hakuna Matata!

Top comments (0)