start learning
Image 1
404747837837837

Javascript Operators

In JavaScript, Operators are symbols or keywords used to perform operations on values or variables. They allow you to manipulate and combine values, perform mathematical calculations, compare values, assign values to variables, and more.

Here are some commonly used JavaScript operators :

Arithmetic Operators:

Addition : +
Subtraction : -
Multiplication : *
Division : /
Modulus (Remainder) : %
Increment : ++
Decrement : --


Assignment Operators :

Assignment : =
Addition assignment : +=
Subtraction assignment : -=
Multiplication assignment : *=
Division assignment : /=
Modulus assignment : %=


Comparison Operators :

Equal to : ==
Not equal to : !=
Strict equal to : ===
Strict not equal to : !==
Greater than : >
Less than : <
Greater than or equal to : >=
Less than or equal to : <=


Logical Operators :

Logical AND : &&
Logical OR : ||
Logical NOT : !


Bitwise Operators :

Bitwise AND : &
Bitwise OR : |
Bitwise XOR : ^
Bitwise NOT : ~
Left shift : <<
Right shift : >>
Zero-fill right shift : >>>


Conditional (Ternary) Operator:
condition ? expression1 : expression2

These are just some of the commonly used operators in JavaScript. There are more operators available for various purposes. Operators are fundamental in writing expressions and performing operations in JavaScript.

step-by-step examples with explanations

Arithmetic Operators

// Step 1: Declare two variables
var num1 = 10;
var num2 = 5;

// Step 2: Perform addition
var sum = num1 + num2;
console.log(sum); // Output: 15

// Step 3: Perform multiplication
var product = num1 * num2;
console.log(product); // Output: 50

// Step 4: Perform increment
num1++;
console.log(num1); // Output: 11

// Step 5: Perform modulus (remainder)
var remainder = num1 % num2;
console.log(remainder); // Output: 1


var num1 = 10;
var num2 = 5;

var sum = num1 + num2;
console.log(sum); // Output: 15

var product = num1 * num2;
console.log(product); // Output: 50

num1++;
console.log(num1); // Output: 11

var remainder = num1 % num2;
console.log(remainder); // Output: 1

Explanation: In this example, we demonstrate various arithmetic operations using arithmetic operators. We declare two variables num1 and num2 with values 10 and 5 respectively. We then perform addition, multiplication, increment, and modulus operations on these variables and log the results to the console.


Comparison Operators

// Step 1: Declare two variables
var num1 = 10;
var num2 = 5;

// Step 2: Perform comparison
var isEqual = num1 == num2;
console.log(isEqual); // Output: false

// Step 3: Perform greater than comparison
var isGreater = num1 > num2;
console.log(isGreater); // Output: true

// Step 4: Perform less than or equal to comparison
var isLessThanOrEqual = num1 <= num2;
console.log(isLessThanOrEqual); // Output: false

Here is the full code :


var num1 = 10;
var num2 = 5;

var isEqual = num1 == num2;
console.log(isEqual); // Output: false

var isGreater = num1 > num2;
console.log(isGreater); // Output: true

var isLessThanOrEqual = num1 <= num2;
console.log(isLessThanOrEqual); // Output: false

Explanation: In this example, we showcase comparison operations using comparison operators. We declare two variables num1 and num2 with values 10 and 5 respectively. We then compare these variables using the equal to, greater than, and less than or equal to operators and store the results in corresponding variables. The results are logged to the console.


Logical Operators

// Step 1: Declare two variables
var isTrue = true;
var isFalse = false;

// Step 2: Perform logical AND
var resultAnd = isTrue && isFalse;
console.log(resultAnd); // Output: false

// Step 3: Perform logical OR
var resultOr = isTrue || isFalse;
console.log(resultOr); // Output: true

// Step 4: Perform logical NOT
var resultNot = !isTrue;
console.log(resultNot); // Output: false


var isTrue = true;
var isFalse = false;

var resultAnd = isTrue && isFalse;
console.log(resultAnd); // Output: false

var resultOr = isTrue || isFalse;
console.log(resultOr); // Output: true

var resultNot = !isTrue;
console.log(resultNot); // Output: false

Explanation: This example demonstrates the logical operations using logical operators. We declare two variables isTrue and isFalse with boolean values. We then perform logical AND, logical OR, and logical NOT operations on these variables and log the results to the console.