DEV Community

Tandap Noel Bansikah
Tandap Noel Bansikah

Posted on

1

Arithmetic Expression in JavaScript

Arithmetic expressions are mathematical expressions that involve arithmetic operations such as addition, subtraction, multiplication, and division. In JavaScript, you can use arithmetic expressions to perform calculations and manipulate numerical values.

Let's take a look at some examples of arithmetic expressions in JavaScript:

  1. Addition:

You can use the + operator to add two or more numbers. For example:


let sum = 5 + 10;
console.log(sum); // Output: 15
Enter fullscreen mode Exit fullscreen mode

2.Subtraction:

You can use the - operator to subtract one number from another. For example:

let difference = 10 - 5;
console.log(difference); // Output: 5
Enter fullscreen mode Exit fullscreen mode

3.Multiplication:

You can use the * operator to multiply two or more numbers. For example:


let product = 5 * 10;
console.log(product); // Output: 50

Enter fullscreen mode Exit fullscreen mode

4.Division:

You can use the / operator to divide one number by another. For example:


let quotient = 10 / 5;
console.log(quotient); // Output: 2
Enter fullscreen mode Exit fullscreen mode

5.Modulo:

You can use the %operator to get the remainder of a division operation. For example:


let remainder = 10 % 3;
console.log(remainder); // Output: 1

Enter fullscreen mode Exit fullscreen mode

You can also use parentheses to group arithmetic expressions and control the order of operations. For example:


let result = (10 + 5) * 2;
console.log(result); // Output: 30

Enter fullscreen mode Exit fullscreen mode

In this example, the addition operation is performed first because it is enclosed in parentheses, and then the multiplication operation is performed.

It's worth noting that JavaScript follows the standard order of operations, which is also known as PEMDAS (Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction). This means that operations within parentheses are performed first, followed by exponents, then multiplication and division (from left to right), and finally addition and subtraction (from left to right).

In conclusion, arithmetic expressions are an important part of JavaScript and are used to perform calculations and manipulate numerical values. By understanding the different arithmetic operators and the order of operations, you can write effective JavaScript code that performs complex calculations.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay