HTML Academy
Console log
Introduction to programming4/15
Back to the list of tasks
  • 1. Career start
  • 2. Making the program a bit more complicated
  • 3. Console log
  • 4. Data types
  • 5. Complex data types
  • 6. Unknown data
  • 7. Variables
  • 8. Declaring and assigning variables
  • 9. Operations
  • 10. Operations order
  • 11. A few more operations
  • 12. Release of Brekkie-meter v0.1, part 1
  • 13. Release of Brekkie-meter v0.1, part 2
  • 14. Summary of “Getting to know JavaScript”
  • 15. First program: MufFit v0.1
Complex data types
  • Sign up
  • Log in

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.

Comments

  • script.js
JavaScript

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted

Use the console.log() command to show the values below in the console and check their types. For each value, enter a separate log in the console with a new line:

  1. 100
  2. 1.5
  3. 'Muffin says hi'
  4. true

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2025

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.