A large difference exists between a variable and a string-type literal (data types will be discussed later).
Assume you want to print the "like" word in the console. If you run the following command, you will get an error:
ReferenceError tells you that the variable
like is not defined.
The interpreter looks for a variable, function, or keyword with the name
Variables in JS have some data: name, address to the cell in memory, data type, and value.
- Name: it's the name of a variable you give when defining it.
- Memory address: when a variable is created, a place in memory is allocated to store it.
- Type: this is a type of data the variable contains.
- Value: this is the data that the variable stores.
Literal contains Type and Value too. The variable gets literal if you need to use it in a function, operator, or something else.
To define a variable you need to use the
let keyword and choose a name for this variable:
Variables should be named in camelCase style. The camelCase means that words are written together without a space, and each word (except the first) must be capitalized.
The new variable contains an
undefined value by default.
undefinedvalue means that nothing contains in a variable.
You can assign and reassign a value to the variable:
Variables are the main core of different programs. You can use every variable an unlimited number of times.
Consider a case when you write 1000 lines of code using literals but make a typo in one word that repeats not once... You need to correct each word in your code and lose more time.
Now, look at the example (but with less number of lines):
Here author lost the
l letter in the
World word. To fix this program you need to correct each line.
Look at the next example:
Correct the example above yourself.
lletter to the
In the example above, string concatenation is used. The string concatenation will be described in the next section.