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.
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
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 |
Example
In this Example:
- The addition operator (
+
) addsa
andb
. - The subtraction operator (
-
) subtractsb
froma
. - The multiplication operator (
*
) multipliesa
andb
. - The division operator (
/
) dividesa
byb
. - The modulus operator (
%
) calculates the remainder of dividinga
byb
. - The increment operator (
++
) increasesa
by one. - The decrement operator (
--
) decreasesb
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
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 |
Example
In this Example:
x == y
checks for value equality, ignoring type, and evaluates to true because5
is equal to"5"
.x === y
checks for value and type equality, returning false sincex
is a number andy
is a string.x != y
andx !== y
demonstrate the not equal and strictly not equal comparisons, showing the difference when considering type.- The
>
,<
,>=
, and<=
operators compare numeric values ofx
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
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 |
Example
In this example:
- The logical AND (
&&
) operator returns false because botha
andb
are not true (b
is false). - The logical OR (
\|\|
) operator returns true because at least one ofa
orb
is true (a
is true). - The logical NOT (
!
) operator negates the value ofb
, 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
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 |
Example
In this example:
- Bitwise AND (
&
) returns 0 because there are no positions where bothc
andd
have a 1. - Bitwise OR (
\|
) returns 7 (111 in binary) because it combines the bits ofc
andd
. - Bitwise XOR (
^
) also returns 7, as it reflects the positions wherec
andd
differ. - Bitwise NOT (
~
) inverts the bits ofd
, resulting in-3
. - Left shift (
<<
) moves the bits ofc
one position to the left, doubling its value. - Right shift (
>>
) and Zero-fill right shift (>>>
) move the bits ofc
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
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 |
Example
In this example:
- Each assignment operator modifies
e
in place, starting withe = 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.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible