In this JavaScript tutorial, we are going to learn about Operators in JavaScript.
What are Operators?
- Operators are symbols used to perform some operations.
- The operation can be performed on one or more than one operand.
Types of Operators
- Unary Operators
- Logical Operators
- Relational Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Bitwise Operators
- Ternary Operator
Types of Operators in JavaScript
1. Unary Operators
- These operators are used on a single variable.
- Unary Operators are used to increment and decrement the value of a variable.
- Unary operators are: ++, -- +, -, ~!
Example:
let num = 20;
num++;
console.log(num);
//Output: 21
2. Logical Operators
- Logical operators are used on one or more than one variable.
- Logical operators return true or false values.
- Logical operators are: &&, ||, !
Example:
let num = 20;
if(num>15 && num<25)
{
console.log("You are Young");
}
//Output: You are Young
3. Relational Operators
- Relational operators describe the relation between two variables.
- These operators return true or false.
- Relational operators are: ==, <, <=, >, >=, !=
Example:
let num = 20;
if(num === 20){
console.log("Num is = 20");
}
else{
console.log("Num is not equal to 20");
}
//Output: Num is = 20
4. Arithmetic Operators
- Arithmetic operators are used to perform arithmetic operations.
- These operators perform the operation and store the new variable.
- These operators are: +, -, *, /, %
Example:
let num1 = 20;
let num2 = 4;
let result = num1 + num2;
console.log(result)
//Output: 24
5. Assignment Operators
- Assignment operators are used to assign value to the variable.
- Assign the value by performing different operations.
- Assignment operators are: =, +=, -=, *=, /=, %=, etc.
Example:
let num1 = 20;
let num2 = 4;
let result = num1 + num2;
console.log(result)
//Output: 24
6. Comparison Operators
- Comparison operators in JavaScript are used to compare two values.
- These return a boolean result indicating whether the comparison is true or false.
- Comparison operators are: ==, !=, ===, !==, >, <, >=, <=
Example:
let num1 = 20;
let num2 = 4;
let result = num1 > num2;
console.log(result)
//Output: true
7. Bitwise Operators
- These manipulate the individual bits of numeric values.
- These perform bitwise operations on binary representations of numbers at the bit level.
- Bitwise operators are: &, |, ^, ~, <<, >>, >>>
Example:
let num1 = 5;
let num2 = 3;
let resultAnd = num1 & num2;
console.log(resultAnd);
//Output: 1
8. Ternary Operator
- The Ternary Operator in JavaScript is also known as the conditional operator.
- It provides a concise way to write conditional expressions.
- The ternary operator allows you to evaluate a condition and return one of two values based on whether the condition is true or false.
Syntax:
condition ? expression1 : expression2;
- The condition is evaluated.
- If the condition is true, then expression1 is executed and returns its value.
- If the condition is false, then expression2 is executed and returns its value.
Example:
let age = 18;
let result = (age >= 18) ? 'You are an Adult' : 'You are not an Adult';
console.log(result);
// Output: 'You are an Adult'