JavaScript From Beginner To Advanced

0% completed

Previous
Next
JavaScript - Operators

Operators in JavaScript are powerful tools that allow developers to perform various operations on values and variables. These operations can range from basic arithmetic to more complex logical comparisons. By understanding and effectively utilizing these operators, developers can implement intricate logic, manipulate data, and evaluate conditions within their applications.

JavaScript operators are categorized based on the functionality they offer, from arithmetic operations that deal with numbers to logical operations that evaluate to true or false. Each category serves a unique purpose, enabling precise control over the flow and outcome of code execution.

Image

JavaScript Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations. These operators can handle everything from addition and subtraction to more advanced calculations like modulus operations.

Table of Arithmetic Operators

OperatorNameDescription
+AdditionAdds two operands
-SubtractionSubtracts second operand from the first
*MultiplicationMultiplies two operands
/DivisionDivides first operand by second
%ModulusReturns remainder of division
++IncrementIncreases operand's value by one
--DecrementDecreases operand's value by one

Example

Javascript
Javascript

. . . .

In this Example:

  • The addition operator (+) adds a and b.
  • The subtraction operator (-) subtracts b from a.
  • The multiplication operator (*) multiplies a and b.
  • The division operator (/) divides a by b.
  • The modulus operator (%) calculates the remainder of dividing a by b.
  • The increment operator (++) increases a by one.
  • The decrement operator (--) decreases b by one.

JavaScript Comparison Operators

Comparison operators compare two values and return a Boolean value (true or false) based on whether the comparison is true.

Table of Comparison Operators

OperatorNameDescription
==Equal toTrue if operands are equal
===Strictly equal toTrue if operands are equal and of the same type
!=Not equal toTrue if operands are not equal
!==Strictly not equal toTrue if operands are not equal or not of the same type
>Greater thanTrue if left operand is greater than the right
<Less thanTrue if left operand is less than the right
>=Greater than or equal toTrue if left operand is greater than or equal to the right
<=Less than or equal toTrue if left operand is less than or equal to the right

Example

Javascript
Javascript

. . . .

In this Example:

  • x == y checks for value equality, ignoring type, and evaluates to true because 5 is equal to "5".
  • x === y checks for value and type equality, returning false since x is a number and y is a string.
  • x != y and x !== y demonstrate the not equal and strictly not equal comparisons, showing the difference when considering type.
  • The >, <, >=, and <= operators compare numeric values of x with other numbers, determining their relational standing.

JavaScript Logical Operators

Logical operators in JavaScript are crucial for making decisions based on multiple conditions. These operators evaluate expressions to a Boolean value (true or false), allowing for complex conditional logic in applications.

Table of Logical Operators

OperatorNameDescription
&&Logical ANDTrue if both operands are true
||Logical ORTrue if at least one operand is true
!Logical NOTTrue if the operand is false

Example

Javascript
Javascript

. . . .

In this example:

  • The logical AND (&&) operator returns false because both a and b are not true (b is false).
  • The logical OR (\|\|) operator returns true because at least one of a or b is true (a is true).
  • The logical NOT (!) operator negates the value of b, turning false into true.

JavaScript Bitwise Operators

Note for first-time coders: Skip the Bitwise Operators section. Jump to JavaScript Assignment Operators

Bitwise operators in JavaScript treat their operands as a set of 32 bits (zeros and ones), rather than decimal, hexadecimal, or octal numbers. These operators are used for low-level programming tasks such as graphics or cryptography.

Table of Bitwise Operators

OperatorNameDescription
&Bitwise ANDEach bit of the result is 1 if both bits are 1
|Bitwise OREach bit of the result is 1 if at least one bit is 1
^Bitwise XOREach bit of the result is 1 if only one of the bits is 1
~Bitwise NOTInverts all the bits
<<Left shiftShifts left by pushing zeros in from the right and let the leftmost bits fall off
>>Right shiftShifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off
>>>Zero-fill right shiftShifts right by pushing zeros in from the left, and let the rightmost bits fall off

Example

Javascript
Javascript

. . . .

In this example:

  • Bitwise AND (&) returns 0 because there are no positions where both c and d have a 1.
  • Bitwise OR (\|) returns 7 (111 in binary) because it combines the bits of c and d.
  • Bitwise XOR (^) also returns 7, as it reflects the positions where c and d differ.
  • Bitwise NOT (~) inverts the bits of d, resulting in -3.
  • Left shift (<<) moves the bits of c one position to the left, doubling its value.
  • Right shift (>>) and Zero-fill right shift (>>>) move the bits of c one position to the right, halving its value, but differ in handling the leftmost bits.

JavaScript Assignment Operators

Assignment operators in JavaScript are used to assign values to variables. The simple assignment operator (=) assigns the right operand's value to the left operand. Other assignment operators modify the variable's value in place according to the operation performed before the assignment.

Note for first-time coders: You may skip the details of any bitwise assignment operators.

Table of Assignment Operators

OperatorNameDescription
=Assignmentx = y
+=Addition assignmentx = x + y
-=Subtraction assignmentx = x - y
*=Multiplication assignmentx = x * y
/=Division assignmentx = x / y
%=Modulus assignmentx = x % y
<<=Left shift assignmentx = x << y
>>=Right shift assignmentx = x >> y
&=Bitwise AND assignmentx = x & y
^=Bitwise XOR assignmentx = x ^ y
|=Bitwise OR assignmentx = x | y

Example

Javascript
Javascript

. . . .

In this example:

  • Each assignment operator modifies e in place, starting with e = 10 and then applying various operations like addition (+=), subtraction (-=), multiplication (*=), and so on.
  • The result of each operation is immediately assigned back to e, demonstrating how assignment operators can simplify in-place modifications of variable values.

Operators in JavaScript are fundamental tools that allow for the manipulation of values, enabling arithmetic calculations, value comparisons, logical operations, and much more. Through this exploration of various types of operators, from arithmetic to assignment, we've seen how they can be applied to control the flow of data and logic within our programs.

Understanding and mastering the use of these operators is essential for any JavaScript developer aiming to build dynamic, efficient, and sophisticated applications. By effectively leveraging operators, developers can implement complex logic with precision and ease, making JavaScript a powerful language for web development.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next