Variables are tools within Javascript that allow you to declare a variable, and assign it some value, for example, a string (a word or letter, eg "Privetiki") Boolean values (True, False), objects, a number etc. Here's one example of its structure:
var greeting;
greeting = "Privetiki";
or
var greeting = "Privetiki";
If you want to be super fancy about it, you can also concatenate, or add together text in this way:
var greeting = "Privetiki" + "tavarishulichki";
Here are some basic operations you can do with variables if you're assigning them numbers:
var number = 5;
number += 1;
// or number = number + 1; or number ++;
which will change the number 5 to 6.
These are also some of the operations you could do with variables. Some alternatives to variables, are the let and const tool.
With the variable, you are able to change its assigned value at any point in the code, and it won't show an error message. The most recent update to the variable value will be its reflected one.
The let function, however, is a more specific version of var, which can only be accessed within its 'block of code'. It should also be assigned a value on the same line of code it was declared on, or it will display an error message, like the const function.
The const function cannot be changed later on in the code, unlike a variable, and will continue to have the same value that was assigned in the beginning, or it will display an error message. It has the same scope as the let tool.
Scope describes the places where a declared variable can be accessed. For example, the variable's scope is global, because it can be accessed anywhere. However, if it is declared within a function, it cannot be accessed outside of it.
Top comments (0)