Javascript
Operators in JavaScript are symbols or keywords that perform operations on operands. These operations can be anything from basic arithmetic calculations to more complex logical evaluations.
JavaScript has lots of helpful operators, and we can group them into different types, each with its own special job.
Arithmetic operators are the mathematicians of JavaScript, responsible for performing various mathematical operations on numeric values. These operations include ‘+’, ‘-’, ‘*’, and ‘/’.
let x = 10;
let y = 5;
let sum = x + y; // Addition
let difference = x - y; // Subtraction
let product = x * y; // Multiplication
let quotient = x / y; // Division
These operators determine the relationship between two values and return a Boolean result. Common comparison operators include greater than (>
), less than (<
), equal to (==
), and not equal to (!=
).
let a = 5;
let b = 10;
console.log(a > b); // Output: false
console.log(a < b); // Output: true
Bitwise operators work with the binary form of numbers, dealing with the 0s and 1s in their makeup. It includes AND (&
), OR (|
), XOR (^
), and NOT (~
).
let m = 5; // Binary: 0101
let n = 3; // Binary: 0011
console.log(m & n); // Bitwise AND: 1
console.log(m | n); // Bitwise OR: 7
Think of logical operators as tools that help computers make decisions. They include logical AND (&&
), logical OR (||
), and logical NOT (!
).
let p = true;
let q = false;
console.log(p && q); // Logical AND: false
console.log(p || q); // Logical OR: true
Assignment operators are responsible for assigning values to JavaScript variables. They include the simple assignment operator (=
) and compound assignment operators like +=
, -=
, and more.
let r = 10;
r += 5; // Equivalent to: r = r + 5;
JavaScript features an array of special operators, each with a distinct purpose. Let’s explore some of these special operators:
a. Conditional (Ternary) Operator (?:)
The conditional operator, often referred to as the ternary operator, provides a concise way to write conditional statements.
let isSunny = true;
let weatherMessage = isSunny ? "Enjoy the sun!" : "Don't forget your umbrella!";
console.log(weatherMessage);
b. Comma Operator ,
The comma operator allows you to include multiple expressions in a single statement, evaluating them from left to right.
let a = 5, b = 10, c = 15;
console.log(a, b, c); // Output: 5 10 15
c. Delete Operator delete
The delete operator removes a property from an object.
let car = { brand: "Toyota", model: "Camry" };
delete car.model;
console.log(car); // Output: { brand: "Toyota" }
d. in
Operator
The ‘in’ operator checks if a specified property is in an object.
let fruits = { apple: "red", banana: "yellow" };
console.log("banana" in fruits); // Output: true
e. instanceof
Operator
The instanceof
operator checks if an object is an instance of a specific class or constructor.
function Car() {}
let myCar = new Car();
console.log(myCar instanceof Car); // Output: true
f. new
Operator
The new
operator creates an instance of a user-defined object type or one of the built-in object types.
function Person(name) {
this.name = name;
}
let john = new Person("John");
console.log(john.name); // Output: John
g. typeof
Operator
The typeof
operator returns a string indicating the type of a variable.
let age = 25;
console.log(typeof age); // Output: number
h. void
Operator
The void
operator evaluates an expression and then returns undefined.
let result = void 0;
console.log(result); // Output: undefined
i. yield
Operator
The ‘yield’ operator is used in generator functions to pause and resume the execution of a function.
function* generateNumbers() {
yield 1;
yield 2;
yield 3;
}
let iterator = generateNumbers();
console.log(iterator.next().value); // Output: 1
Think of operator precedence as a set of rules that helps the computer decide which math operation to do first. Here is a table of operator precedence in JavaScript, arranged from the highest to the lowest precedence.
Precedence | Operator Types | Individual Operators |
---|---|---|
1 | Member Access | . |
2 | Computed Member Access | [] |
3 | Call, Create Instance | (), new |
4 | Increment, Decrement | ++, – |
5 | Logical NOT, Bitwise NOT | !, ~ |
6 | Positive, Negative | +, - |
7 | Exponentiation | ** |
8 | Multiplication, Division, Remainder | *, /, % |
9 | Addition, Subtraction | +, - |
10 | Bitwise Shifts | <<, >>, >>> |
11 | Relational, Instanceof, in | <, >, <=, >=, instanceof, in |
12 | Equality | == , !=, ===, !== |
13 | Bitwise AND | & |
14 | Bitwise XOR | ^ |
15 | Bitwise OR | ` |
16 | Logical AND | && |
17 | Logical OR | ` |
18 | Conditional (Ternary) Operator | ? : |
19 | Assignment | =, +=, -=, *=, /=, %= and others |
20 | Comma | , |