DEV Community

Cover image for Let's Learn JS Toghether - Variables
Veronika Khachatryan
Veronika Khachatryan

Posted on

Let's Learn JS Toghether - Variables

What are Variables?
We already know what they are from basic mathematical skills right? In Java Script Variables are containers to store data values for computer to understand and utilize later on.

There are 3 types of variables: var, let and const

Image description.

After Brendan Eich developed Java Script in 1995, variable "var" was mostly used. Afterwards developers created the variable "let", which has mostly the same characteristics as variable "var" did. Nevertheless, they differ in a significant aspect for which it's recommended to use "let" only, instead of "var".
Compared to "let", "var" can be redeclared. If we write
var a = 5367
var a = 7634
console.log(a)

Our output will be 7634. Nevertheless with let, we are not able to do this kind of staff, as the program will say that "a" is already declared.

What about variable "const"?
Logical right? It's constant. The constant variable is not alterable as soon as we assigned it to some value. It cannot be reassigned or redeclared.

On Contrast to "const", we are able to reassign the values stored in variables "var" and "let".

Image description

In JavaScript, the equal sign (=) is an "assignment" operator, not an "equal to" operator. So we ASSIGN a value to a variable with "=".

Image description

If we are declaring variables with the same "let", we are allowed to write "let" for the first variable and skip the "let" part for the following declarations. For instance :
let a = 6, b = 8, c = 10;

And do not forget to separate them with commas.

How to name variables?
There are some rules to name them:
1.Names can contain digits, letters, underscores and dollar signs.
2.They can start with a letter, underscore or a dollar sign.
3.Names are case sensitive. Upper case at the begining of a name is not allowed.

let capital0fCountry = "Yerevan",
x = 1757989,
c = "helllooo everyone",
b = "We are learning JS with Vika",

A limitation for the names of variables is that we cannot name them with numbers like this ( let 10 = "smth"), because in our further code we might use the number 10, which will cause an error as we will confuse the computer whether 10 is a variable or a number.

Similar to Mathematics, we can do some Arithmetic operations with variables. We can add, substruct, multiply or divide the values of the variables.

Image description

Image description

Image description

If one variable's value is a number, but the other one's is a text (string), JS will convert the number into a string and concatenate the values of variables in case of addition.

Image description

The Visibility of variables determines the ***Scope* {}.
Scope is a space where our assigned values are **accessible
. Another difference between "let" and "var" shows up here. The variable "var" is visible everywhere. On the other hand the variable "let" and "const" are visible only in the scope where we declared them.

Java Script starts to read the code starting from down then goes to the top if needed.
If the JS does not see the required variable in the scope, it goes up, finds and console.log's the last declared variable.

That's all for variables. See you in my next blog post about Functions. Have a great time learning!

Top comments (0)