HTML Academy
Declaring and assigning variables
Introduction to programming9/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
Operations order
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Operations

In order to write universal programs, we had to learn the following:

  1. get previously unknown data,
  2. save this data to variables for further use,
  3. process data or perform operations on them.

We are now done with the first two items. Let’s proceed to operations now. Take a look at this command:

50 + 100;

This is an operation. It consists of an operation sign, +, and two operands, 50 and 100.

The sign defines what is going to happen to the operands. In the command above, we use a plus sign, which means that it’s an addition operation and the operands will be added up. The command will return the addition result, 150.

Addition is a binary operation since it only works with two operands. Binary operations are the most commonplace ones. However, there are unary operations with just one operand and ternary operations with three operands.

We can use variables as operands:

breakfastCalories + dinnerCalories;
milkInGrams * 0.5;
50 + waterInGrams;

Different operations can be performed on different types of operands, so it’s important to understand the types of data stored in variables. The console.log() command really helps here.

Let’s practice on numerical operations. Here are some of them:

Addition+
Subtraction-
Multiplication*
Division/

Comments

  • script.js
JavaScript
var milkInGrams = 50; var waterInGrams = 100;

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

Send the results of operations with variables to the console but don’t change the values of variables.

  1. Add up two variables to get the total amount of liquid that Muffin drank.
  2. Multiply milkInGrams by 0.03 to calculate the amount of protein.
  3. Divide milkInGrams by 20 to calculate the amount of carbs.
  4. Subtract 150 from the sum of variables to calculate the deviation from the daily norm.

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.