Learn JavaScript
  • Introduction
  • Basics
    • Comments
    • Variables
    • Types
    • Equality
  • Numbers
    • Math
    • Basic Operators
    • Advanced Operators
  • Strings
    • Creation
    • Replace
    • Length
    • Concatenation
  • Conditional Logic
    • If
    • Else
    • Switch
    • Comparators
    • Concatenate
  • Arrays
    • Unshift
    • Map
    • Spread
    • Shift
    • Pop
    • Join
    • Length
    • Push
    • For Each
    • Sort
    • Indices
  • Loops
    • For
    • While
    • Do...While
  • Functions
    • Higher Order Functions
  • Objects
    • Properties
    • Mutable
    • Reference
    • Prototype
    • Delete
    • Enumeration
    • Global footprint
  • Linked List
    • Add
    • Pop
    • Prepend
    • Shift
  • Browser Object Model (BOM)
    • Window
    • Popup
    • Screen
    • Navigator
    • Cookies
    • History
    • Location
  • Date and Time
  • JSON
  • Error Handling
    • try...catch...finally
  • Events
  • Regular Expression
  • Modules
  • Debugging
  • Classes
    • Static
    • Inheritance
    • Access Modifiers
  • Promise, async/await
    • Async/Await
  • Miscellaneous
    • Hoisting
    • Currying
    • Polyfills and Transpilers
  • Exercises
    • Console
    • Multiplication
    • User Input Variables
    • Constants
    • Concatenation
    • Functions
    • Conditional Statements
    • Objects
    • FizzBuzz Problem
    • Get the Titles!
Powered by GitBook
On this page

Was this helpful?

  1. Numbers

Advanced Operators

When operators are put together without parenthesis, the order in which they are applied is determined by the precedence of the operators. Multiplication (*) and division (/) has higher precedence than addition (+) and subtraction (-).

// multiplication is done first, which is then followed by addition
let x = 100 + 50 * 3; // 250
// with parenthesis operations inside the parenthesis are computed first
let y = (100 + 50) * 3; // 450
// operations with the same precedences are computed from left to right
let z = 100 / 50 * 3;

Some advanced operators can be used, such as:

  • Modulus (division remainder): x = y % 2

  • Exponential: Given a= 5, c = a**2, Results: c= 25

  • Increment: Given a = 5

    • c = a++, Results: c = 5 and a = 6

    • c = ++a, Results: c = 6 and a = 6

  • Decrement: Given a = 5

    • c = a--, Results: c = 5 and a = 4

    • c = --a, Results: c = 4 and a = 4

Nullish coalescing operator '??'

The nullish coalescing operator returns the first argument if it's not null/undefined, else the second one. It is written as two question marks ??. The result of x ?? y is:

  • if x is defined, then x,

  • if y isn’t defined, then y.

It's a recent addition to the language and might need polyfills to support old browsers

PreviousBasic OperatorsNextStrings

Last updated 2 years ago

Was this helpful?