0% completed
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.
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.
Operator | Name | Description |
---|---|---|
+ | Addition | Adds two operands |
- | Subtraction | Subtracts second operand from the first |
* | Multiplication | Multiplies two operands |
/ | Division | Divides first operand by second |
% | Modulus | Returns remainder of division |
++ | Increment | Increases operand's value by one |
-- | Decrement | Decreases operand's value by one |
In this Example:
+
) adds a
and b
.-
) subtracts b
from a
.*
) multiplies a
and b
./
) divides a
by b
.%
) calculates the remainder of dividing a
by b
.++
) increases a
by one.--
) decreases b
by one.Comparison operators compare two values and return a Boolean value (true or false) based on whether the comparison is true.
Operator | Name | Description |
---|---|---|
== | Equal to | True if operands are equal |
=== | Strictly equal to | True if operands are equal and of the same type |
!= | Not equal to | True if operands are not equal |
!== | Strictly not equal to | True if operands are not equal or not of the same type |
> | Greater than | True if left operand is greater than the right |
< | Less than | True if left operand is less than the right |
>= | Greater than or equal to | True if left operand is greater than or equal to the right |
<= | Less than or equal to | True if left operand is less than or equal to the right |
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.>
, <
, >=
, and <=
operators compare numeric values of x
with other numbers, determining their relational standing.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.
Operator | Name | Description |
---|---|---|
&& | Logical AND | True if both operands are true |
|| | Logical OR | True if at least one operand is true |
! | Logical NOT | True if the operand is false |
In this example:
&&
) operator returns false because both a
and b
are not true (b
is false).\|\|
) operator returns true because at least one of a
or b
is true (a
is true).!
) operator negates the value of b
, turning false into true.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.
Operator | Name | Description |
---|---|---|
& | Bitwise AND | Each bit of the result is 1 if both bits are 1 |
| | Bitwise OR | Each bit of the result is 1 if at least one bit is 1 |
^ | Bitwise XOR | Each bit of the result is 1 if only one of the bits is 1 |
~ | Bitwise NOT | Inverts all the bits |
<< | Left shift | Shifts left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Right shift | Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
>>> | Zero-fill right shift | Shifts right by pushing zeros in from the left, and let the rightmost bits fall off |
In this example:
&
) returns 0 because there are no positions where both c
and d
have a 1.\|
) returns 7 (111 in binary) because it combines the bits of c
and d
.^
) also returns 7, as it reflects the positions where c
and d
differ.~
) inverts the bits of d
, resulting in -3
.<<
) moves the bits of c
one position to the left, doubling its value.>>
) 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.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.
Operator | Name | Description |
---|---|---|
= | Assignment | x = y |
+= | Addition assignment | x = x + y |
-= | Subtraction assignment | x = x - y |
*= | Multiplication assignment | x = x * y |
/= | Division assignment | x = x / y |
%= | Modulus assignment | x = x % y |
<<= | Left shift assignment | x = x << y |
>>= | Right shift assignment | x = x >> y |
&= | Bitwise AND assignment | x = x & y |
^= | Bitwise XOR assignment | x = x ^ y |
|= | Bitwise OR assignment | x = x | y |
In this example:
e
in place, starting with e = 10
and then applying various operations like addition (+=
), subtraction (-=
), multiplication (*=
), and so on.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.
.....
.....
.....