Conversation


Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0


advertisement

The Hidden Power of JavaScript Operators

JavaScript Operators

An operator specifies the type of operation to be performed on the values of variables and expressions. JavaScript provides different types of operators to perform simple to complex calculations and evaluations.

Certain operators are also used to construct relational and logical statements. These statements allow implementation decisions and looping constructs.


1. Basics of Operators

An operation is an action performed on one or more values stored in variables. The specified action either changes the variable's value or generates a new value. This means an operation requires at least one symbol and some value. Here, the symbol is called an operator and specifies the action type to be performed on the value. The value or variable on which the operation is performed is called an operand. For example, X*2 is an expression, where X and 2 are operands and * is an operator. This operator specifies that the multiplication operation is to be performed on the operands.

There are three main types of operators, which are as follows:

  • Unary operators - Operate on a single operand. For example, the expression y = -X.

  • Binary operators - Operate on two operands. For example, the expression Sum = y + X.

  • Ternary operators - Operates on three operands. For example, the expression age >= 18 ? "Eligible" : "Not eligible".




Operators and their Types

Operators help in simplifying expressions. JavaScript provides a predefined set of operators that allow performing different operations. JavaScript operators are classified into six categories based on the type of action they perform on operands.

These six categories of operators are as follows:

  • Arithmetic Operators
  • Relational operators
  • Logical operators
  • Assignment operators
  • Bitwise operators
  • Special operators


1. Arithmetic Operators

Arithmetic operators are binary operators, as they perform basic arithmetic operations on two operands. The operator appears between the two operands, which allows you to perform computations on numerical and string values. These computations include addition, subtraction, multiplication, and division.

Lists the arithmetic operators with their descriptions and an example of each type.

  • + (Addition) - Performs addition. In the case of string values, it behaves as a string concatenation operator and appends a string at the end of the other
  • - (Subtraction) - Performs subtraction. If a larger value is subtracted from a smaller value, it returns a negative numerical value.
  • / (Division) - Divides the first operand by the second operand and returns the quotient
  • % (Module) - Divides the first operand by the second operand and returns the remainder
  • * (Multiplication) - Multiplies the two operands


Example
  • 35+76
  • 96-48
  • 34/3
  • 90%20
  • 67*4



2. Increment and Decrement Operators

The increment and decrement operators are unary operators, as they operate only on a single operand. The increment operator (++) increases the value by 1, while the decrement operator (--) decreases the value by 1. These operators can be placed either before or after the operand. If the operator is placed before the operand, the expression is called pre-increment or pre-decrement. If the operator is placed after the operand, the expression is called post-increment or post-decrement.

The use of increment and decrement operators by assuming that the numOne variable's value is 2.

Expression - Type - Result

  • numTwo = ++nunOne; - Pre-increment - numTwo = 3
  • numTwo = numOne++; - Post-increment - numTwo = 2
  • numTwo = --numOne; - Pre-decrement - numTwo = 1
  • numTwo = numOne--; - Post-decrement - numTwo = 2



3. Relational Operator

Relational operators are binary operators that make a comparison between two operands. After making a comparison, they return a boolean value namely, true or false. The expression consisting of a relational operator is called a relational expression or a conditional expression.

Lists the relational operators along with their descriptions and an example of each type.

  • == (Equal) - Verifies whether the two operands are equal
  • != (Not Equal) - Verifies whether the two operands are unequal
  • === (Strict Equal) - Verifies whether the two operands are equal and whether they are of the same type.
  • !== (Strict Not Equal) - Verifies whether the two operands are unequal and whether they are not of the same type.
  • > (Greater Than) - Verifies whether the left operand is greater than the right operand
  • < (Less Than) - Verifies whether the left operand is less than the right operand
  • >= (Greater Than or Equal) - Verifies whether the left operand is greater than or equal to the right operand
  • <= (Less Than or Equal) - Verifies whether the left operand is less than or equal to the right operand


Example
  • 90 == 91
  • 99 !=98
  • 3 === 4
  • 3 !== "3"
  • 97 > 95
  • 94 <96
  • 92 >= 93
  • 99 <= 100



4. Logical Operator

Logical operators are binary operators that perform logical operations on two operands. They belong to the category of relational operators, as they return a boolean value.

Lists various logical operators and an example of each type, assuming that x is 2 and y is 2.

  • && (AND) - Returns true, if either of the operands are evaluated to true. If the first operand evaluates to true, it will ignore the second operand.
  • ! (NOT) - Returns false if the expression is true and vice-versa.
  • || (OR) - Returns true if either of the operands is evaluated to true. If the first operand evaluates to true, it will ignore the second operand.


Example

  • (x 2) && (y 5) and Returns false
  • !(x ==3) Returns true
  • If(x 2) || (y 5) Returns true



5. Assignment Operator

The assignment operator assigns the value of the right-side operand to the operand on the left side by using the equal operator (-).

The assignment operator is divided into two categories in JavaScript which are as follows:

  • Simple assignment operator - This is the operator that is used to assign a value or result of an expression to a variable. For example, result = numOne + numTwo;

  • Compound assignment operator - This is formed by combining the simple assignment operator with the arithmetic operators. For example, salary -=eval(salary * tax / 100);


The use of the assignment operator by assuming the value of the variable numOne as 6.

  • numOne += 6; - numOne = numOne + 6
  • numOne -= 6; - numOne = numOne - 6
  • numOne = 6; - numOne = numOne * 6
  • numOne %= 6; - numOne = numOne % 6
  • numOne /= 6; - numOne = numOne / 6


Result

  • numOne = 12
  • numOne = 0
  • numOne = 36
  • numOne = 0
  • numOne = 1



6. Bitwise Operator

Bitwise operators represent their operands in bits (zeros and ones) and perform operations on them. However, they return standard decimal values.

Lists various bitwise operators along with their descriptions and an example of each type, assuming a is 9 (00001001) and b is 14 (00001110).

  • & (Bitwise AND) - Compares two bits and returns 1 if both of them are 1 or else returns 0.
  • ~ (Bitwise NOT) - Inverts each bit of the operand, changes its sign, and subtracts by 1. It is a unary operator.
  • I (Bitwise OR) - Compares two bits and returns 1 if the corresponding bits of either or both the operands are 1.
  • ^ (Bitwise XOR) - Compares two bits and returns 1 if the corresponding bit of either, but not both of the operands is 1.


Example

  • a & b Returns8 (00001000)
  • ~a Returns -10
  • a | b Returns 15 (00001111)
  • a ^ b Returns 7 (00000111)



7. Special Operator

Some operators in JavaScript do not belong to any of the categories of JavaScript operators. Such operators are referred to as special operators.

Lists the most commonly used special operators in JavaScript.

  • . (comma) - Combines multiple expressions into a single expression, operates on them in left to right order, and returns the value of the expression on the right.

  • ?: (conditional) - Operates on three operands where the result depends on a condition. It is also called a ternary operator and has the form condition? value1:value2. If the condition is true, the operator obtains value1 or else obtains value2.

  • typeof - Returns a string that indicates the type of the operand. The operand can be a string, variable, keyword, or an object. 


JavaScript JavaScript Operators Operators Operators in JavaScript Hidden Power of Javascript operators Basics of operators operators and their types javascript operators types categories of operators in js

advertisement