Select Page

Operators are used in JavaScript (and programming in general) to assign and compare values, perform arithmetic operations, evaluate expressions, and more. JavaScript supports both binary and unary operators, as well as a unique ternary operator called the conditional operator. A binary operator needs two operands — one before the operator and one after it.

black flat screen computer monitor

Assignment Operators

The operator “assignment” assigns the value of its right operand to the left of the operand based on the use/value of its own. The assignment operator is basically equal (=), which sets the value of the right operand in place of its left operand. That is, x = f() is an assignment expression that assigns f() to x.

Destructuring

For more complex chores, the destructuring assignment syntax is a JavaScript expression that allows you to pull information from arrays or objects using a syntax that mirrors array and object literals’ construction.

Evaluation And Nesting

In most cases, classes and functions are defined inside a variable declaration (e.g., const, let, or var), although they can also be used as standalone statements.

Comparison Operators

A comparison operator evaluates its operands and returns a logical value if the comparison is true. Numerical, string, logical, and object values can all be used as operands. Unicode strings are compared in accordance with standard lexicographical order. In most situations, if the two operands aren’t of the same type, JavaScript will attempt to convert them to a type that can be compared.

This behavior generally results in the operands being compared numerically. The only conditions in which comparisons may not be converted are the === and !== operators, which perform strict equality and inequality checks. These operators do not attempt to convert the inputs to compatible types before testing for equality.

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numbers. Unary operators, such as + and -, are used with the ++ and — operators. It only works with a left or right operand. When applied to x, for example, x++ increases the value of x when the program control passes to the next statement. When it is paired with the correct operator, such as ++x, it will only increase the value of x in that case. As a result, x++ is known as post-increment, while +x is known as pre-increment.

Unary plus (+): Unary plus are used to convert the operand to a number if it isn’t already.

Unary negation (-): The unary negation operator returns the negation of its argument.

Increment (++): The addition of one is performed on the operand. If used as a prefix operator, it returns the value of its operand after adding one; if used as a postfix operator, it returns the value of its operand before adding one.

Decrement (–): Subtracts a number from its argument. The return value is similar to that of the increment operator.

The remainder (%): The integer remainder of dividing the two operands is returned.

Exponentiation operator (**): The calculation of the base to the exponential power is known as exponentiation.

Bitwise Operators

It treats its operands as a set of 32 bytes/bits rather than as hexadecimal, decimal, or octal numbers. The decimal number nine has a binary representation of 1001, for example. It works on binary representations but returns standard numerical values in JavaScript.

Bitwise Logical Operators

The logical operators function bitwise as follows:

  • The arguments are converted to 32 bit integers and presented as a sequence of bits (zeros and ones). The most significant bits of numbers with more than 32 bits are removed.
  • The bits in the first operand are connected to the bits in the second operand by pairing: first bit to first bit, second bit to second bit, and follows.
  • The operation is performed on each pair of bits, and the resulting bitwise value is calculated.

Bitwise Shift Operators

The bitwise shift operators work with two input values: the first is a quantity that is to be shifted, and the second is the number of bit positions by which it should be shifted. The operator used determines whether shifting is forward or backward.

The 32-bit integer result is the result of a shift operation. The type of the left operand determines the return value: BigInt if the type of the left operand is BigInt, and Number otherwise.

The shift operators are listed as follows:

Left shift (<<): The left shift operator locates the first operand one position to the left. Overflowing bits are discarded as a result of this operation. Bits are shifted in from the right with zero being shifted out.

Sign-propagating right shifts: The right-shift operator shifts the first operand a certain number of bytes/bits to the right. Overflowing bits are discarded to the left. Bits from the leftmost bit/byte are transferred from the left.

Zero-fill right shift (>>>): The operator shifts the first argument to the right a specified number of bits. To the right, any extra bits are discarded. Bits are shifted in from the left, with zero bits being moved over.

Logical Operators

The logical operators are used to merge two or more conditions in JavaScript. The following logical operators are available in JavaScript.

Logical AND (&&): The AND operator is a logical and mathematical operator that checks to see whether two values are non-zero (0, false, undefined, null, or “are considered to be zero) and evaluates one if it is. It returns 1 if the first parameter is non-zero; otherwise, it returns 0.

Logical OR (||): The OR operator is a comparison operator that compares two values, evaluating both of them to see whether any one of them is non-zero (which is considered as zero). It returns 1 if at least one of the two operands is not zero; otherwise, it returns 0.

Logical NOT (!): The NOT operator is a logical operation that flips the result of an expression. It reverses the boolean conclusion of the argument (or test). true becomes false, and false becomes true.

Comma Operator

The comma operator (,) evaluates both of its arguments and returns the value of the most recent operand. This operator is typically utilized in a for loop to allow for the changing of numerous variables each time through the loop. When you do not need it, avoid using it elsewhere. When possible, two separate claims should be used instead of one.

Ternary Operator

Ternary operators are JavaScript’s way of expressing if-then-else statements. The ternary operator “:?” determines a value for a variable based on a condition. This is the abbreviated form of the if-else statement.

Does the ternary operator begin with a conditional expression and the “?” operator. The second portion (after ?) will be carried out if the condition is true. If the condition is false, the third part (after 🙂 will be done.