OPERATORS IN JAVASCRIPT:
Operators in JavaScript are special symbols or keywords used to perform operations on values and variables. They are used for calculations, assigning values, comparing values, and making logical decisions.
For example:
`let a = 10;
let b = 5;
let result = a + b;
console.log(result); // 15`
Types of Operators in JavaScript
JavaScript mainly provides the following types of operators:
1.Arithmetic Operators
2.Assignment Operators
3.Comparison Operators
4.Logical Operators
5.Increment and Decrement Operators
6.Ternary Operator
7.String Operators
8.Bitwise Operators
9.Type Operators
1. Arithmetic Operators
Used to perform mathematical calculations.
Operators: +, -, , /, %, *
Example:
`let a = 10;
let b = 5;
console.log(a + b); // 15`
2. Assignment Operators
Used to assign values to variables.
Operators: =, +=, -=, *=, /=, %=
Example:
`let a = 10;
a += 5;
console.log(a); // 15`
3.Comparison Operators
Comparison operators compare two values and return a Boolean
Example:
console.log(10 > 5); // true
console.log(10 < 5); // false
console.log(10 >= 10); // true
console.log(5 <= 10); // true
4. Logical Operators
Used to combine multiple conditions.
Example:
AND &&
Both conditions must be true.
let age = 20;
let hasID = true;
console.log(age >= 18 && hasID);
Result:
true
Example:
let age = 20;
if (age >= 18 && age <= 60) {
console.log("Allowed");
}
OR ||
At least one condition must be true.
let isStudent = false;
let isEmployee = true;
console.log(isStudent || isEmployee); // true
NOT !
Reverses a Boolean value.
let loggedIn = true;
console.log(!loggedIn); // false
5.String Operators
The + operator can be used to join strings.
This is called concatenation.
Example:
`let firstName = "John";
let lastName = "Doe";
let fullName = firstName + " " + lastName;
console.log(fullName);
`
Top comments (0)