Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Arrays Intro
Making Arrays
Array Builder Kata
Array Elements
Human Names Kata
Array Length
Longest Array Kata
Looping Over Arrays
Highest Number Kata
Word Hunter Kata
Array Average Kata
Min-Max Kata
Adding Elements
Number Range Kata
Removing Elements
Arrays of Objects
Biggest Animal Kata
Arrays of Arrays
Taboo Words Kata
Going Further
Essential JavaScript
Introduction
JavaScript Basics
Numbers
Strings
Logic
Variables
Loops
Objects
Arrays
Arrays Intro
Making Arrays
Array Builder Kata
Array Elements
Human Names Kata
Array Length
Longest Array Kata
Looping Over Arrays
Highest Number Kata
Word Hunter Kata
Array Average Kata
Min-Max Kata
Adding Elements
Number Range Kata
Removing Elements
Arrays of Objects
Biggest Animal Kata
Arrays of Arrays
Taboo Words Kata
Going Further
Shorthand Math
We have used the addition operator to add two numbers:
1 + 2
We have also used the assignment operator to add to an existing variable:
var sum = 0; sum = sum + 2;
JavaScript gives us a shorthand for this operation:
var sum = 0; sum += 2;
This is called the addition assignment operator. Notice how we don't have to repeat the variable name when we use this shorthand.
Shorthand Operators List
All of the common math operations we have worked with so far have a shorthand version:
Operator | Symbol |
---|---|
Addition Assignment | += |
Subtraction Assignment | -= |
Multiplication Assignment | *= |
Division Assignment | /= |
Modulus Assignment | %= |
Using these operators can help make your code more readable.
Practice
There is code in the editor that uses the long form operators. Update the code to use the shorthand operators.
Learning Goals
I can do math operations in JavaScript without repeating the variable name
Code Editor
Click "Run Code" to execute your JavaScript in a secure sandbox and see the output below.
Console Output
// Console output will appear here...
0 output lines