Operator and operands are two keywords used in programming to perform multiple types of operation, this operation can be of different types such as addition, subtraction, multiplication, division, etc.
The operands are the entity to which the operation is performed. In contrast, the operators are the entity that performs that operation and decides the operation type, for example, the + operator performs addition whereas the – operator performs subtraction.
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- typeof Operator
- Ternary Operator
Arithmetic Operators are the operator used to perform arithmetic operations. The arithmetic operation included addition(+), subtraction(-), multiplication(*), division(/), modulus(%), increment(++), and decrement(–).
let x = 1; let y = 2; let z = x + y; console.log("Addition: " + z);
Here, we have initialised two variables with some values and then use the + operator to perform addition and print the output in the console.
|+=||Add and Assign|
|-=||Subtract and Assign|
|*=||Multiply and Assign|
|/=||Divide and Assign|
|%=||Modulus and Assign|
let x = 2; let y = x; // assign value of x to y console.log(y); y += y; // add y then assign total value to y console.log(y);
Here we have used the = operator to assign the value of x to y, then used the += operator which first adds the current value of y to itself and then assigns it to itself.
|!==||Not Strict equal|
|>=||Greater than or equal to|
|<=||Less than or equal to|
let x = 2; let y = 3; let compare = (x < y); console.log(compare);
Here we have compared two variables x and y using the < operator, which returns true if the value of x is smaller than y, and returns false if it is not.
Logical Operators are used to perform logical AND, OR and NOT operations.
let x = true; let y = false; let result = x && y; console.log(result);
Here we have used && operator which returns true only if both side values are equal, if not then returns false.
This operator is used to get the type of a variable.
let x = 2; let type = typeof x; console.log(type);
Want to get the type of Object as well? Don’t worry we have a separate tutorial on it, consider reading it.
The ternary operator is used for conditional execution. It separates a condition and two expressions with a question mark (?), and the two expressions are also separated with a colon (:), it first checks for the condition, if true then executes the first expression and, if false then executes the second expression.
let x = 2; let y = 3; let ternaryOperator = x < y ? "x is less than y" : "x is greater than y"; console.log(ternaryOperator);
x is less than y