Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Data types
The boss is happy now! Of course, the calories didn’t go anywhere, but the program works just fine: shows the results and the tips.
Before we proceed to the next task, let’s get a few things clear. Let’s go back to the results of the program that are shown in the console:
LOG: "Lunch calorie value" (string)
LOG: 320 (number)
LOG: "Dinner calorie value" (string)
LOG: 480 (number)
<= undefined
Each string that begins with LOG:
is the result of the console.log()
command.
The last line begins with the symbol <=
. It shows the value that the program returns after execution. What does “return” mean? The program can execute the code and return, that is, to give its result for further use.
For example, you are talking to a friend on a phone:
“What does two plus two equal?” You ask.
“Four, ” the friend replies quickly.
“And now multiply this value by two!” You say.
With his answer, your friend “returned” you the “four” value. You got an answer to your question and were able to do something with this answer (asked to multiple the result by two). The same is done using the 2 + 2
command. It adds two numbers and returns the 4
value (here everything is logical).
But if you, during the telephone conversation, asked your friend to multiply two by two and write down the result on a piece of paper, you would not have heard “four” as a response. The friend would have written down the answer on paper, but you would not have been able to do anything with this result. console.log ()
works in the same way. You ask the command to log the sum of the numbers console.log (2 + 2)
, it logs 4
in the console, and that’s it. Just like your friend, who simply writes down the answer, without telling you anything. It turns out that in fact console.log ()
either doesn’t return anything or returns “nothing”. This “absence of value” in JavaScript is called undefined
. And that’s what you see in the last line.
The console.log()
command also logs additional information on our console. For example, it shows the type of logged data. You can perform different actions with different data types, so a developer needs to know what data types he or she is working with. That’s what console.log()
helps with. On our console, the data type is in parentheses, for example (string)
or (number)
.
Let’s send different values to the console and see their types.
- script.js
Thanks! We’ll fix everything at once!
Comments