Onjsdev

Share


Javascript Operators


By onjsdev

Feb 11th, 2024

Operators are essential in any programming language to perform arithmetic and logical operations on variables, and JavaScript is no exception.

In this article, we will cover two types of operators in JavaScript: mathematical and comparison operators. Let's get started.

Mathematical Operators

JavaScript provides a range of mathematical operators that perform arithmetic operations on numerical values.

Here is a list of some of the most commonly used mathematical operators in JavaScript:

Addition Operator (+):

The addition operator performs addition between two numerical values.

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

Subtraction Operator (-)

The subtraction operator performs subtraction between two numerical values.

let a = 10;
let b = 5;
let diff = a - b;
console.log(diff); // Output: 5

Multiplication Operator (*)

The multiplication operator performs multiplication between two numerical values.

let a = 5;
let b = 10;
let prod = a * b;
console.log(prod); // Output: 50

Division Operator (/)

The division operator performs division between two numerical values.

let a = 10;
let b = 5;
let quot = a / b;
console.log(quot); // Output: 2

Modulus Operator (%)

The modulus operator returns the remainder of a division operation between two numerical values.

let a = 10;
let b = 3;
let rem = a % b;
console.log(rem); // Output: 1

Comparison Operators

JavaScript also provides a range of comparison operators that compare two values and return a Boolean value, true or false.

Here is a list of some of the most commonly used comparison operators in JavaScript:

Equal to (==)

The equal to operator compares two values and returns true if they are equal, false otherwise.

let a = 5;
let b = 5;
console.log(a == b); // Output: true

Not equal to (!=)

The not equal to operator compares two values and returns true if they are not equal, false otherwise.

let a = 5;
let b = 10;
console.log(a != b); // Output: true

Greater than (>)

The greater than operator compares two values and returns true if the left operand is greater than the right operand, false otherwise.

let a = 10;
let b = 5;
console.log(a > b); // Output: true

Less than (<)

The less than operator compares two values and returns true if the left operand is less than the right operand, false otherwise.

let a = 5;
let b = 10;
console.log(a < b); // Output: true

Greater than or equal to (>=)

The greater than or equal to operator compares two values and returns true if the left operand is greater than or equal to the right operand, false otherwise.

let a = 10;
let b = 5;
console.log(a >= b); // Output: true

Less than or equal to (<=)

The less than or equal to operator compares two values and returns true if the left operand is less than or equal to the right operand, false otherwise.

let a = 5;
let b = 10;
console.log(a <= b); // Output: true

Conclusion

In summary, JavaScript provides a range of mathematical and comparison operators that are fundamental in performing various tasks in JavaScript programming.

Thank you for reading.