Basic Math and Arithmetic Operations

JavaScript is a versatile language often used for web development, but at its core, it can handle arithmetic operations with ease. This post will cover the basic math and arithmetic operations you can perform in JavaScript, including various operators, their usage, and code examples to illustrate their functions.

Basic Arithmetic Operators

In JavaScript, the basic arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), modulus (%), increment (++), and decrement (--). Let's dive into each of these operators with examples.

Addition (+)

The + operator adds two numbers together.

let a = 10; let b = 5; let sum = a + b; // sum will be 15 console.log(sum);

Subtraction (-)

The - operator subtracts the second number from the first number.

let a = 10; let b = 5; let difference = a - b; // difference will be 5 console.log(difference);

Multiplication (*)

The * operator multiplies two numbers.

let a = 10; let b = 5; let product = a * b; // product will be 50 console.log(product);

Division (/)

The / operator divides the first number by the second number.

let a = 10; let b = 5; let quotient = a / b; // quotient will be 2 console.log(quotient);

Modulus (%)

The % operator returns the remainder of the division of two numbers.

let a = 10; let b = 3; let remainder = a % b; // remainder will be 1 console.log(remainder);

Increment (++)

The ++ operator increases the value of a variable by one.

let a = 10; a++; // a will be 11 console.log(a);

Decrement (--)

The -- operator decreases the value of a variable by one.

let a = 10; a--; // a will be 9 console.log(a);

Compound Operators

JavaScript also supports compound operators that combine arithmetic operations with assignment.

Addition Assignment (+=)

let a = 10; a += 5; // a will be 15 console.log(a);

Subtraction Assignment (-=)

let a = 10; a -= 5; // a will be 5 console.log(a);

Multiplication Assignment (*=)

let a = 10; a *= 5; // a will be 50 console.log(a);

Division Assignment (/=)

let a = 10; a /= 5; // a will be 2 console.log(a);

Modulus Assignment (%=)

let a = 10; a %= 3; // a will be 1 console.log(a);

Exponentiation Operator (**)

The ** operator raises the first operand to the power of the second operand.

let a = 2; let power = a ** 3; // power will be 8 (2^3) console.log(power);

This is equivalent to using the Math.pow function:

let a = 2; let power = Math.pow(a, 3); // power will be 8 console.log(power);

Operator Precedence

Operator precedence determines the order in which operations are executed. Multiplication and division have higher precedence than addition and subtraction. You can override the precedence using parentheses.

Without Parentheses

let a = 10 + 2 * 5; // a will be 20 (2*5 is done first, then 10 + 10) console.log(a);

With Parentheses

let a = (10 + 2) * 5; // a will be 60 (10 + 2 is done first, then 12*5) console.log(a);

Special Mathematical Functions

JavaScript's Math object provides a variety of functions for more complex mathematical operations.

Square Root

let a = 16; let sqrt = Math.sqrt(a); // sqrt will be 4 console.log(sqrt);

Absolute Value

let a = -10; let absValue = Math.abs(a); // absValue will be 10 console.log(absValue);

Ceiling and Floor

Math.ceil rounds a number up to the nearest integer, while Math.floor rounds a number down to the nearest integer.

let a = 4.7; let b = 4.3; let ceilValue = Math.ceil(a); // ceilValue will be 5 let floorValue = Math.floor(b); // floorValue will be 4 console.log(ceilValue); console.log(floorValue);

Random Number

You can generate a random number between 0 and 1 using Math.random.

let randomNumber = Math.random(); console.log(randomNumber); // randomNumber will be a float between 0 and 1

For a random integer between 0 and a specified number, you can use:

let max = 100; let randomInt = Math.floor(Math.random() * max); console.log(randomInt); // randomInt will be an integer between 0 and 99

Conclusion

Understanding basic math and arithmetic operations in JavaScript is crucial for any developer. The language supports a wide range of arithmetic operators and functions, providing the tools necessary to perform complex calculations. Whether you're building a simple calculator or handling more complex mathematical logic, JavaScript's arithmetic capabilities will have you covered. Keep this guide handy as a reference, and don't hesitate to experiment with the code examples provided to deepen your understanding. Happy coding!

Start Building in JavaScript Today

Lilbots is the best platform for creating powerful JavaScript bots

  • Built in access to cutting edge AI models
  • Out of the box integration with hundreds of APIs
  • Collaborate with coding workspaces
  • Discover and fork thousands of bots built by the community

Learn JavaScript

JavaScript Objects

JavaScript Arrays

JavaScript Basics