JavaScript Operators

JavaScript Operators

JavaScript Operators

In JavaScript, operators are used to perform operations on values, such as arithmetic operations, comparison operations, logical operations, and more. Here are some of the most commonly used operators in JavaScript:

  1. Arithmetic operators: Used to perform mathematical operations on numbers.

Examples:

				
					let x = 5;

let y = 3;

console.log(x + y); // Addition:
Output: 8

console.log(x - y); //
Subtraction: Output: 2

console.log(x * y); //
Multiplication: Output: 15

console.log(x / y); // Division:
Output: 1.6666666666666667

console.log(x % y); // Modulo
(remainder): Output: 2

console.log(x** y); // Exponentiation: Output: 125
				
			

       2. Comparison operators: Used to compare values and return a boolean result.

Examples:

				
					let x = 5;

let y = 3;


console.log(x > y); // Greater
than: Output: true

console.log(x < y); // Less
than: Output: false

console.log(x >= y); //
Greater than or equal to: Output: true

console.log(x <= y); // Less
than or equal to: Output: false

console.log(x == y); //
Equality: Output: false

console.log(x != y); //
Inequality: Output: true
				
			

       3. Logical operators: Used to combine multiple conditions and return a boolean result.

Examples:

				
					let x = 5;

let y = 3;

let z = 7;

console.log(x > y &&
x < z); // AND: Output: true

console.log(x > y || x >
z); // OR: Output: true

console.log(!(x> y)); // NOT: Output: false
				
			

       4. Assignment operators: Used to assign values to variables.

Examples:

				
					let x = 5;

x += 3; // Addition assignment:
Equivalent to x = x + 3

console.log(x); // Output: 8

let y = 7;

y *= 2; // Multiplication
assignment: Equivalent to y = y * 2

console.log(y); // Output: 14
				
			

These are just a few examples of the many operators available in JavaScript. By using operators, you can perform a wide range of operations on values and build more complex and sophisticated programs.

Arithmetic:

Arithmetic operators are used to perform mathematical operations on numeric values in JavaScript. The basic arithmetic operators are:

  1. Addition (+): Adds two or more values together.

Example:

				
					let x = 5;
let y = 3;
console.log(x + y); // Output: 8
				
			

2. Subtraction (): Subtracts one value from another.

Example:

  1. Multiplication (*): Multiplies two or more values together.

Example:

				
					let x = 5;

let y = 3;

console.log(x * y); // Output:15
				
			

4.    Division (/): Divides one value by another.

Example:

				
					let x = 5;



let y = 3;


console.log(x/ y); // Output: 1.6666666666666667
				
			
  1. Modulo (%): Returns the remainder of dividing one value by another.

Example:

				
					let x = 5;

let y = 3;

 

console.log(x % y); // Output: 2
				
			
  1. Exponentiation (**): Raises one value to the power of another.

Example:

				
					let x = 5;

let y = 3;

console.log(x ** y); // Output:
125
				
			

You can use these operators in JavaScript to perform a wide variety of arithmetic operations on numeric values, which is particularly useful for working with math and scientific computations.

Join To Get Our Newsletter
Spread the love