Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Variables
The program has received the data and it needs to save it somewhere for future use. To do this, we first need to deal with processing of data. It is obvious that the data is saved in the computer’s memory. Memory is a complex structure using complex addresses. In the past, memory operations looked like this:
put 0xEC002...0xEC003 1 // saved number 1 to a memory cell
get 0xEC002...0xEC003 // loaded number 1 from a memory cell
It’s very inconvenient to constantly use these complicated addresses. It’s very hard to memorize what was saved and why. This is why lazy developers came up with a simple solution: variables.
put my_number 1 // saved number 1 to the variable my_number
get my_number // loaded number 1 from the variable my_number
A variable is just a name for data that can be made understandable to people. And this name can be written in different ways. These are the two of the most common ways: camelCase (camel notation) and snake_case (snake notation). In the first case, all words in the variable name are written together and each new word begins with a capital letter (myNumber
, userName
). In the second case, all words are separated by underscore (my_number
, my_name
). In the courses, we will use camelCase and write the names of variables exactly like this.
Variables simplify memory operations: they stick to memory cells like a name sticker does to a document folder. Variables let you save, retrieve and modify data.
In JavaScript, variables can be created using the var
command followed by the name of a variable:
var variableName;
After creating a variable, you can use it in other commands and output it to the console:
// Please note that there are no quotes here!
console.log(variableName);
- script.js
Thanks! We’ll fix everything at once!
Comments