Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Assignment Operators in JavaScript | Performing Operations in JavaScript
Introduction to JavaScript
course content

Kursinnehåll

Introduction to JavaScript

Introduction to JavaScript

1. JavaScript Fundamentals
2. Variables and Data Types in JavaScript
3. Performing Operations in JavaScript
4. Controlling Program Flow with Conditional Statements
5. Looping Through Data in JavaScript
6. Functions in JavaScript

book
Assignment Operators in JavaScript

Readability in code is essential, and JavaScript offers ways to make your code more elegant. In this chapter, we'll explore operations with assignments, which can streamline your code.

JavaScript provides several assignment operators:

  • Addition Assignment (+=);
  • Subtraction Assignment (-=);
  • Multiplication Assignment (*=);
  • Division Assignment (/=);
  • Remainder (Modulo) Assignment (%=);
  • Exponentiation Assignment (**=).

Assignment operators are used to enhance code readability.

Consider the following example:

123
let a = 17; a += 5; console.log(a);
copy

This code is equivalent to the following:

123
let a = 17; a = a + 5; console.log(a);
copy

The expression a += 5 accomplishes the same as a = a + 5.

Let's look at other assignment operators and their default counterparts:

With AssignmentDefault
a += 6a = a + 6
a -= 6a = a - 6
a *= 6a = a * 6
a /= 6a = a / 6
a %= 6a = a % 6
a **= 6a = a ** 6
question mark

Choose the correct result:

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 3
Vi beklagar att något gick fel. Vad hände?
some-alt