Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Performing Arithmetic On Variables | Section
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
JavaScript Essentials for Beginners - 1768407374405

bookPerforming 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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 22

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

bookPerforming Arithmetic On Variables

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 22
some-alt