Js Arithmetic & Comparison Operators

Arithmetic operators

Arithmetic operators in JavaScript are used to perform basic mathematical operations on numbers. Here are the common arithmetic operators along with examples:

1. Addition (+):

The addition operator adds two numbers together.

var num1 = 5;
var num2 = 10;
var sum = num1 + num2; // sum will be 15

2. Subtraction (-):

The subtraction operator subtracts the second number from the first.

var num1 = 20;
var num2 = 8;
var difference = num1 - num2; // difference will be 12

3. Multiplication (*):

The multiplication operator multiplies two numbers.

var num1 = 4;
var num2 = 6;
var product = num1 * num2; // product will be 24

4. Division (/):

The division operator divides the first number by the second.

var num1 = 50;
var num2 = 10;
var quotient = num1 / num2; // quotient will be 5

5. Modulus (%):

The modulus operator gives the remainder of the division between the first number and the second.

var num1 = 15;
var num2 = 4;
var remainder = num1 % num2; // remainder will be 3

6. Exponentiation ( or Math.pow()):**

The exponentiation operator raises the first number to the power of the second.

var base = 2;
var exponent = 3;
var result = base ** exponent; // result will be 8
// Alternatively, using the Math.pow() function
var result = Math.pow(base, exponent); // result will also be 8

These arithmetic operators can be used with variables or literal values to perform mathematical calculations in JavaScript. They follow the standard rules of arithmetic and operator precedence. Keep in mind that JavaScript also supports combined assignment operators (e.g., +=, -=, *=, /=) that allow you to perform an operation and assignment in a single step.

Comparison operators

Comparison operators in JavaScript are used to compare values and determine whether certain conditions are true or false. Here are the common comparison operators along with examples:

1. Equal (==):

Checks if two values are equal.

var num1 = 5;
var num2 = 5;
console.log(num1 == num2); // true

2. Not Equal (!=):

Checks if two values are not equal.

var num1 = 5;
var num2 = 10;
console.log(num1 != num2); // true

3. Strict Equal (===):

Checks if two values are equal in both value and data type.

var num1 = 5;
var str = "5";
console.log(num1 === str); // false

4. Strict Not Equal (!==):

Checks if two values are not equal in either value or data type.

var num1 = 5;
var str = "5";
console.log(num1 !== str); // true

5. Greater Than (>):

Checks if the value on the left is greater than the value on the right.

var num1 = 10;
var num2 = 5;
console.log(num1 > num2); // true

6. Less Than (<):

Checks if the value on the left is less than the value on the right.

var num1 = 5;
var num2 = 10;
console.log(num1 < num2); // true

7. Greater Than or Equal To (>=):

Checks if the value on the left is greater than or equal to the value on the right.

var num1 = 10;
var num2 = 10;
console.log(num1 >= num2); // true

8. Less Than or Equal To (<=):

Checks if the value on the left is less than or equal to the value on the right.

var num1 = 5;
var num2 = 10;
console.log(num1 <= num2); // true

These comparison operators return a boolean value (true or false) based on whether the comparison is true or false. They are commonly used in conditional statements (like if statements) to control the flow of a program based on certain conditions.