Assignment
Assignment is the process in which a value is placed inside a variable, as previously discussed in variable declarations. There are many different assignment operators, the most common of which is the "=" operator, like this: let x = 42
. Here are some other common operators that you may see and find useful:
Name
Purpose
Shorthand
Addition Assignment
x = x + y
x += y
Subtraction Assignment
x = x - y
x -= y
Multiplication Assignment
x = x * y
x *= y
Division Assignment
x = x / y
x /= y
Remainder Assignment
x = x % y
x %= y
Exponential Assignment
x = x ** y
x **= y
The exponential operator **
is a newly introduced feature of ES7, or ES 2016. In mathematical notation, it would be written as x ^ y (or x to the power of y), and it acts the same as the Math.exp()
function. However, it has not yet been standardized for use in production, so it may not work as intended in all browsers. Use it with caution.
String Operators
Some assignment operators can also be used with strings, such as let x = 'testing'
. String concatenation is another example of this.
File Location
We will be working in the following file:
Practice
In
assignment.js
, create a variable and assign it a value.Use each of the shorthand assignment operators and print the results of each to the console.
Create another variable and assign it a string value.
Use each assignment operator on this variable and print the results to the console.
Are these results what you expected? Why or why not?
Last updated