JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • String Operators
  • File Location
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 5-Expressions and Operators

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:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
            └── 2-ControlFlow-and-ErrorHandling
            └── 3-Loops
            └── 4-Functions
            └── 5-Expressions-and-Operators
                01-assignment.js <----You will be working in this file.

Practice

  1. In assignment.js, create a variable and assign it a value.

  2. Use each of the shorthand assignment operators and print the results of each to the console.

  3. Create another variable and assign it a string value.

  4. Use each assignment operator on this variable and print the results to the console.

  5. Are these results what you expected? Why or why not?

Previous5-Expressions and OperatorsNextComparison

Last updated 7 years ago