• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Basics / Assignment Operators in JavaScript

Assignment Operators in JavaScript

Assignment Operators in JavaScript

JavaScript has several assignment operators that can be used to assign values to variables. These operators are as follows:

The “=” operator: This is the most basic assignment operator and is used to assign a value to a variable. For example:

let x = 5;

The “+=” operator: This operator is used to add the value on the right side of the operator to the value of the variable on the left side, and then assign the result to the variable on the left side. For example:

let x = 5;
x += 3; // x is now 8

The “-=” operator: This operator is used to subtract the value on the right side of the operator from the value of the variable on the left side, and then assign the result to the variable on the left side. For example:

let x = 5;
x -= 3; // x is now 2

The “*=” operator: This operator is used to multiply the value on the right side of the operator by the value of the variable on the left side, and then assign the result to the variable on the left side. For example:

let x = 5;
x *= 3; // x is now 15

The “/=” operator: This operator is used to divide the value of the variable on the left side by the value on the right side of the operator, and then assign the result to the variable on the left side. For example:

let x = 15;
x /= 3; // x is now 5

The “%=” operator: This operator is used to divide the value of the variable on the left side by the value on the right side of the operator, and then assigns the remainder to the variable on the left side. For example:

let x = 15;
x %= 3; // x is now 0

The “**=” operator: This operator is used to raise the value of the variable on the left side by the power of the value on the right side of the operator.

let x = 2;
x **= 3; // x is now 8

These operators are a shorthand way to perform an operation and assign the result to a variable in a single step, which can make your code more concise and readable.

Basics

Related Snippets:

  • The onabort Event in JavaScript
  • JavaScript Arrays Explained
  • The JavaScript function Math.pow()
  • The JavaScript function Math.floor()

Primary Sidebar

FREE UPDATES!

Get the latest updates in your inbox for FREE!

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2023 JavaScriptSource.com

  • About
  • Privacy Policy
  • Submit
  • FAQ
  • Jobs For Developers
  • Contact Us