JavaScript Arithmetic Operators

Updated on August 28, 2019
Sam Shepards profile image

I'm a software developer with a great interest in data analysis and statistics.

Arithmetic operators are used to perform basic arithmetic on numbers (integers, floating points, etc.). They also perform some actions or manipulation on strings. The operators are mostly the same as in many other programming languages. More on data types in JavaScript.

In JavaScript, there are differences in how arithmetic operators function in some particular cases, especially when we compare to strongly typed languages such as Java. JavaScript Arithmetic operators:

JavaScript Arithmetic Operators

Modulo or remainder

Addition Operator +

The addition operator adds numbers or concatenates strings, depending on the datatypes of the operands.

2 + 3 // 5
"Hi, " + "you" // "Hi, you"
"2" + "3" // "23"

As you’ve seen in the example above, when the values of both operands are numbers it will be basic arithmetic addition. When both are strings, then it’s also pretty much obvious what the + operator does, namely gluing the operands together, also known as concatenation.

In many other cases, type conversion is needed and the result depends on the position of the operands. The addition operator will set the priority to string concatenation. If one of the operands is a string, then the result will be a string.

If you perform addition on a pair of operands that are of the number type and then you add a string, it will first perform the arithmetic on the numbers and next it will concatenate with the String (“3 eyes” example below).

If you have a number type with the addition operator followed by a string, another addition operator and then a number again, then it will concatenate the first number with the string, then concatenate that result with the last number (“1 eyes 2” example below).

"2" + 3 // "23" concatenation
2 + "3" // "23" concatenation
1 + 2 + " eyes" // "3 eyes" the two numbers add and then concatenation
1 + " eyes " + 2 // "1 eyes 2"
1 + ( 2 + " eyes") // "12 eyes" parentheses define priority

Subtraction Operator –

The subtraction operator works like normal arithmetic. If you subtract and one or more of your operands are strings of non-numeric characters you’ll get NaN (Not a Number) as a result.

If you subtract two strings with only numeric characters, you’ll get a number, like normal subtraction of numbers. If one operand is a string with only numeric characters and the other operand is a number you’ll get a number as a result.

2 - 3 // -1
"2" - "3" // -1
6 - "4" // 2
"6" + "3" - "2" // 61 first concatenation then substraction

Multiplication Operator *

It takes two operands and multiplies them, just like basic math. If both operands are numerical strings, they will be coerced to numbers (type conversion) and the result will be multiplication of those numbers. The same thing will happen if one operand is of type number and the other is a numerical string.

If one or more operands are non-numerical (words, alphabetical chars,…) strings we get NaN (Not a Number).

2 * 3 // 6
"2" * 3 // 6
"2" * "3" // 6
2 * "P" // NaN

Division Operator /

The division operator takes number operands and will return a number, float or “Infinity” (if the later operand is zero) after dividing them. Type conversion will occur for numerical strings. For non-numerical string, we will get NaN, similar to multiplication and subtraction.

2 / 3 // 0.66666666666666
"2" / 3 // 0.66666666666666
"2" / 3 // 0.66666666666666
2 / 0 // Infinity
2 / "text" // NaN

Modulo Or Remainder Operator %

This special modulo operator finds the remainder after division of one number of another. Here we also have type conversion for numerical strings and NaN when non-numerical strings are involved.

2 % 3 // 2
3 % 2 // 1
"2" % 3 // 2

Increment Operator ++

The increment operator adds 1 to its single operand. The operator will add 1 to the value of the variable (let) and then assigns the result back to the variable.

There are two types of increment operators: pre-increment and post-increment operators. Pre-increment will increment the operand and evaluates to the incremented value immediately. While post-increment will increment its operand but will still evaluate to the unincremented value of that operand when assigned to another variable at the same time.

let a = 1, b = a++;  // a is 2 and b is 1 (first the value of a is assigned to b, only afterwards a is incremented)
let a = 1, b = ++a;  // a and b are 2
9-- // will throw an uncaught reference error, so the value has to be assigned to a variable.
let x = "test";
x++ // NaN   Not a Number

Decrement Operator – –

The decrement operator will convert the value of the operand to a number (if needed and possible), subtract 1, and assign the result into the operand (variable). Similar to the increment operator, the decrement has two types: pre-decrement and post-decrement.

let a = 1, b = a--;  // a is 0 and b is 1
let a = 1, b = --a;  // a and b are 0

It is worth to note that the expression ++a is not always the same as a = a + 1. The increment operator and decrement operator always convert the operand first.

let a = "66";
--a //  65
let a = "66";
a = a + 1; // 661

© 2019 Sam Shepards


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)