Arithimatic Operators In Javascript
Arithmetic operators are used in programming languages to
perform mathematical operations on numerical values. JavaScript,
being a dynamic and loosely typed language, provides a variety
of arithmetic operators to manipulate numerical values in
various ways.
In this article, we will discuss the various arithmetic
operators available in JavaScript, along with examples of how to
use them.
Addition Operator (+)
The addition operator is used to add two or more numerical
values together. It is denoted by the + symbol. Here's an
example of how to use the addition operator in JavaScript:
let x = 5;
In this example, we're adding the values of x and y together and
storing the result in the variable z.
let y = 3;
let z = x + y; // z is 8
Subtraction Operator (+)
The subtraction operator is used to subtract one numerical value
from another. It is denoted by the - symbol. Here's an example
of how to use the subtraction operator in JavaScript:
let x = 5;
In this example, we're subtracting the value of y from the value
of x and storing the result in the variable z.
let y = 3;
let z = x - y; // z is 2
Multiplication Operator (*)
The multiplication operator is used to multiply two or more
numerical values together. It is denoted by the * symbol. Here's
an example of how to use the multiplication operator in
JavaScript:
let x = 5;
In this example, we're multiplying the values of x and y
together and storing the result in the variable z.
let y = 3;
let z = x * y; // z is 15
Division Operator (/)
The division operator is used to divide one numerical value by
another. It is denoted by the / symbol. Here's an example of how
to use the division operator in JavaScript:
let x = 10;
In this example, we're dividing the value of x by the value of y
and storing the result in the variable z.
let y = 2;
let z = x / y; // z is 5
Modulus Operator (%)
The modulus operator is used to find the remainder of dividing
one numerical value by another. It is denoted by the % symbol.
Here's an example of how to use the modulus operator in
JavaScript:
let x = 10;
In this example, we're finding the remainder of dividing the
value of x by the value of y and storing the result in the
variable z.
let y = 3;
let z = x % y; // z is 1
Increment Operator (++)
The increment operator is used to increase the value of a
numerical variable by one. It is denoted by the ++ symbol.
Here's an example of how to use the increment operator in
JavaScript:
let x = 5;
In this example, we're using the increment operator to increase
the value of the variable x by one.
x++; // x is now 6
Decrement Operator (--)
The decrement operator is used to decrease the value of a
numerical variable by one. It is denoted by the -- symbol.
Here's an example of how to use the decrement operator in
JavaScript:
let x = 5;
In this example, we're using the decrement operator to decrease
x--; // x is now 4
Summary:
In summary, arithmetic operators in JavaScript are used to
perform mathematical operations on numerical values. There are
six arithmetic operators in JavaScript: addition (+),
subtraction (-), multiplication (*), division (/), modulus (%),
increment (++), and decrement (--). Understanding these
operators is essential when working with numerical data in
JavaScript.