HTML Academy
Unknown data
Introduction to programming7/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
Declaring and assigning variables
  • Sign up
  • Log in

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);

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
  1. Create a variable named milkInGrams.
  2. Then output this variable to the console.

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.