Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Performing Arithmetic On Variables | Manipulating Data
Introduction to JavaScript
course content

Conteúdo do Curso

Introduction to JavaScript

Introduction to JavaScript

1. Getting Started
2. Manipulating Data
3. Conditional Statements

book
Performing Arithmetic On Variables

We can perform arithmetic on variables storing numbers similar to how we perform operations on raw numerical values.

For example:

1234
let varA = 10; let varB = 20; console.log(varA + varB);
copy

The expression varA + varB considers varA and varB as if they were raw numbers.

The right-hand side of the assignment statement is always evaluated before feeding the result into the variable on the left side. Because of this, we can make changes to the value of a variable:

1234
let exampleVar = 100; console.log(exampleVar); // Before exampleVar = exampleVar + 100; console.log(exampleVar); // After
copy

This above code adds 100 to the value of exampleVar.

However, the same operation can also be performed using a shorter syntax:

123
let exampleVar = 100; exampleVar += 100; console.log(exampleVar);
copy

This syntax works for any supported arithmetic operation.

For example:

1234
exampleVar *= 100; // Multiplication exampleVar /= 100; // Division exampleVar **= 100; // Raising to a Power exampleVar %= 100; // Remainder
copy

A short syntax for incrementing the value of a variable by 1 is by using the ++ operator. For-example:

1234
let varA = 9; console.log(varA); // Before varA++; console.log(varA); // After
copy

We can use -- for decrementing a variable by 1:

1234
let varA = 5; console.log(varA); // Before varA--; console.log(varA); // After
copy

1. What will the following code output?

2. What is the purpose of the following line of code?

3. Which of the following is the correct shorthand for the operation exampleVar = exampleVar - 50?

4. What will the following code output?

question mark

What will the following code output?

Select the correct answer

question mark

What is the purpose of the following line of code?

Select the correct answer

question mark

Which of the following is the correct shorthand for the operation exampleVar = exampleVar - 50?

Select the correct answer

question mark

What will the following code output?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 8
Sentimos muito que algo saiu errado. O que aconteceu?
some-alt