Operators are important tools in JavaScript that help developers work with data. There are different types of operators, and each type has a special job.
Arithmetic Operators:
These are used for basic math. They include:
+
)-
)*
)/
)%
, which gives the remainder).let sum = 5 + 3;
, it means we're adding 5 and 3, which equals 8
.Assignment Operators:
These are used to give values to variables. Some examples are:
=
(basic assignment)+=
(add and assign)-=
(subtract and assign).let x = 5; x += 2;
, we start with x
as 5 and then add 2, making x
equal to 7
.Comparison Operators:
These help us make decisions by comparing values. They give a true or false answer. Some common ones are:
==
(equal to)===
(strictly equal to)<
(less than)>
(greater than).if (a === b) { // do something }
checks if a
is exactly the same as b
.Logical Operators:
These are used to combine true/false statements. They include:
&&
(AND)||
(OR)!
(NOT).if (isActive && isVerified) { // proceed }
, which means both conditions must be true to continue.Ternary Operator:
This is a quick way to write an if-else
statement. It looks like this:
condition ? expr1 : expr2
.
For example, let status = isLoggedIn ? "Logged In" : "Guest";
sets status
to either "Logged In" or "Guest" based on if the user is logged in.
Bitwise Operators:
These work with data at a very basic level, using binary code (1s and 0s). Some examples are &
, |
, and ^
. Bitwise operators aren't used as much, but they can be helpful in certain situations.
Knowing how to use these operators is really important for coding well in JavaScript. They help us change and manage data, making it easier to create more complex programs.
Operators are important tools in JavaScript that help developers work with data. There are different types of operators, and each type has a special job.
Arithmetic Operators:
These are used for basic math. They include:
+
)-
)*
)/
)%
, which gives the remainder).let sum = 5 + 3;
, it means we're adding 5 and 3, which equals 8
.Assignment Operators:
These are used to give values to variables. Some examples are:
=
(basic assignment)+=
(add and assign)-=
(subtract and assign).let x = 5; x += 2;
, we start with x
as 5 and then add 2, making x
equal to 7
.Comparison Operators:
These help us make decisions by comparing values. They give a true or false answer. Some common ones are:
==
(equal to)===
(strictly equal to)<
(less than)>
(greater than).if (a === b) { // do something }
checks if a
is exactly the same as b
.Logical Operators:
These are used to combine true/false statements. They include:
&&
(AND)||
(OR)!
(NOT).if (isActive && isVerified) { // proceed }
, which means both conditions must be true to continue.Ternary Operator:
This is a quick way to write an if-else
statement. It looks like this:
condition ? expr1 : expr2
.
For example, let status = isLoggedIn ? "Logged In" : "Guest";
sets status
to either "Logged In" or "Guest" based on if the user is logged in.
Bitwise Operators:
These work with data at a very basic level, using binary code (1s and 0s). Some examples are &
, |
, and ^
. Bitwise operators aren't used as much, but they can be helpful in certain situations.
Knowing how to use these operators is really important for coding well in JavaScript. They help us change and manage data, making it easier to create more complex programs.