What are Operators?
Operators are special symbols used to perform operations on operands, which can be variables or values. For example, in 13 / 3, the “/” division symbol serves as the operator, while “13” and “3” serve as operands.
A value or variable on which operators conduct operations is known as an operand. This is a little exercise to put yourself to the test. Examine the code snippet below and distinguish between operators and operands:
1. 2 + 5 * 3
Types of Operators in JavaScript:-
Not all operators in JavaScript perform arithmetic operations. Rather, operators vary in their functions. In this section we will look at some of the different types of operators and how they work in JavaScript.
Arithmetic Operators:-
An arithmetic operator is used to perform basic mathematical operations. It takes numerical values as operands, which can be variables or literals, and returns a value.
Arthmetic operators in JavaScript include:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Exponentiation (**)
- Modulus (%)
- Decrement (–)
- Increment (++)
- Division (/)
Comparison Operators:
A comparison operator compares two operands and returns a Boolean (true or false) value as a result of the comparison.
The JavaScript comparison operators are as follows:
- Equals (==)
- Not Equal (!=)
- Strict Equal (===)
- Strict Not Equal (!==)
- Greater than (>)
- Greater than or equal (>=)
- Less than (<)
- Less than or equal (<=)
Assignment Operators:
an assignment operator in JavaScript is like a tool that helps you give a value to a container (variable). The most basic assignment operator is the equal sign (=), and it works by taking the value on its right side and putting it into the container on its left side. It’s like saying, “Hey, take this value and store it in that variable.” So, it’s a fundamental way to assign or set values to variables in your code.
Let’s look at some further assignment operator examples:
- Assignment (=)
- Addition Assignment (+=)
- Subtraction Assignment (-=)
- Division Assignment (/=)
- Multiplication Assignment (*=)
- Exponentiation Assignment (**=)
- Modulus Assignment (%=)
Logical Operators:
When used with boolean values, a logical operator produces a boolean value (true or false), otherwise it returns the value of one of the operands. Logical operators are used to check and determine the logic between two or more operands.
- OR (| |)
- AND (&&)
- NOT (!)
Examples of the operators in use are provided below:
// USING THE AND(&&) OPERATOR
let canDrive = true;
let hasLicense = false;
const readyToDrive = canDrive && hasLicense;
console.log(readyToDrive) //OUTPUT: false
The AND (&&) operator determines this because if one of the requirements is false, the operation’s result will also be false. The operation cannot produce a truthy value unless both values are true
// USING THE AND(&&) OPERATOR
let canDrive = true;
let hasLicense = true;
const readyToDrive = canDrive && hasLicense;
console.log(readyToDrive) // OUTPUT: true
Next, given the identical scenario as earlier, let’s investigate how we can use the OR(||) operator:
// USING THE OR(||) OPERATOR
let canDrive = true;
let hasLicense = false;
const readyToDrive = canDrive || hasLicense;
console.log(readyToDrive) // OUTPUT: true
thank you………………………………………