VOOZH about

URL: https://www.digitalocean.com/community/tutorials/how-to-do-math-in-javascript-with-operators

⇱ How To Do Math in JavaScript with Operators | DigitalOcean


How To Do Math in JavaScript with Operators

Updated on August 25, 2021
👁 How To Do Math in JavaScript with Operators

Introduction

Mathematical operations are among the most fundamental and universal features of any programming language. In JavaScript, numbers are used frequently for common tasks such as finding browser window size dimensions, getting the final price of a monetary transaction, and calculating the distance between elements in a website document.

Although a high-level understanding of mathematics is not a prerequisite to being a capable developer, it is important to know what types of operations are available in JavaScript, and how to use math as a tool to accomplish practical tasks.

Unlike other programming languages, JavaScript only has one number data type; there is no distinction made between integers (positive or negative whole numbers) and floats (numbers with a decimal point), for example.

In this tutorial, we will go over arithmetic operators, assignment operators, and the order of operations used with JavaScript number data types.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

Tutorial Series: How To Code in JavaScript

JavaScript is a high-level, object-based, dynamic scripting language popular as a tool for making webpages interactive.

About the author(s)

Software engineer and open source creator

Community and Developer Education expert. Former Senior Manager, Community at DigitalOcean. Focused on topics including Ubuntu 22.04, Ubuntu 20.04, Python, Django, and more.

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Nice article. I think this example will better than when run in Console tab of browser.

let y = 7;

y++;
//result: 7
y++;
//result: 8

console.log(y);
//result: 9

Hi! What will be the explaination for this set of codes: var a = 10; var b = 10; sum = Math.log2(2a * 2b); console.log(sum); sum = 20;

Tania , your tutorials are very nice. In the addition and subtraction section Please include this example for scrollToId function.

<!DOCTYPE html> <html lang=“en”> <head> <meta charset=“UTF-8”> <title>Title</title> <style> div { border: 1px solid black; background-color: lightblue; height: 2000px; width: 2000px; } </style> </head> <body>

<button onclick=“myFunction()” style=“position:fixed;”>Click me to go down</button><br><br> <button onclick=“scrollToId()” style=“position:fixed;”>Click me to go up</button><br><br> <div></div>

<script> function scrollToId() { const navHeight = 100; window.scrollTo(0, window.pageYOffset - navHeight); }

function myFunction() { window.scrollTo(0, 100); alert("pageXOffset: " + window.pageXOffset + ", pageYOffset: " + window.pageYOffset); } </script>

</body> </html>

Thank you .

I think this explanation has a mistake(or ambiguity).

Set a variable

let y = 7;

// Use the prefix increment operation
let postfix = y++;

console.log(postfix);
Output
7

The value of y was not increased in the postfix operation. This is because the value will not be incremented until after the expression has been evaluated.

Actually, when using the postfix ++ operator the value of y gets updated but what is returned to the left-hand side is the old value of y.

let postfix=y++;
//can be thought of
//let postfix=y;
//y=y+1;
👁 Creative Commons
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
  • Deploy on DigitalOcean

    Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and AI-native businesses

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

© 2026 DigitalOcean, LLC.Sitemap.
Dark mode is coming soon.