Operators
📚 Table of Contents |
---|
Arithmetic |
Assignment |
Comparison |
Logical |
- ok so, I almost skipped this page bc its not very exciting and a bunch of this is kind of self explainatory... buuuut some are kind of important, especially the logical operators
- you've already seen operators in action a few times in this crash course
- operators are pretty much the backbone of any logic you create
- I've heard operators described as the "verbs", or action words if JavaScript was a written language
Arithmetic Operators
- these are your basic math operators like
+
,-
,*
, and/
- they do exactly what you expect: add, subtract, multiply, and divide
- there are a few more to be aware of though:
**
creates an exponent%
is NOT percent, it's themodulo
ormodulus
; which is the division remainder- to non-mathematicians & non-traditional comp sci people, this one can be a little intimidating and a little weird. maybe a little why tf do I need this
- the
modulo
is actually great for finding if a number is odd or even, which is surprisingly common. - check out this function. it creates a function called
isOddOrEven
that takes a number as a parameter. the code then checks if themodulo
or remainder is0
when the number is divided by2
. if yes, the number is even, if not the number is odd. then we check the function passing in whatever number we want as an argument and console log it
function isOddOrEven(number) {
if (number % 2 === 0) {
return "Even";
} else {
return "Odd";
}
}
console.log(isOddOrEven(5)); // Outputs Odd
console.log(isOddOrEven(8)); // Outputs Even
- two more that are good to know are
++
and--
, which increment (add one) and decrement. you see these a lot in for loops
Assignment Operators
- we've seen these already used for variable declaration
- basic assign (
=
): assigns a value to a variable
let x = 5;
- the next two are sort of shorthand
- add and assign (
+=
): adds a value and then assigns it.x += 3
is the same asx = x + 3
- subtract and assign (
-=
): Subtracts a value and then assigns it.x -= 2
is the same asx = x - 2
- you can also do this with
*
,/
,%
, and**
too... odds of seeing these in the wild is rare but know its possible if you need/want to
Comparison Operators
-
here are a few more that need no explaination:
<
,>
,<=
,>=
- less than (the "L"), greater than, less than or equal to, and greater than or equal to
-
theres also the JS ternary operator
?
- this is far from necessary for begginners but good to know about
?
is actually a little weird at first but I love it and use it all the time- you use
?
as an alternative to an "if else" statement, or to do "if else" logic on one line 🧑🍳😘 chefs kiss - to do that you test the thing you want to know about, use the ternary, define the logic for if true then a (
:
) if its false: - syntax:
condition ? ifTrue : ifFalse
- just to make things just a little more confusing, there are question marks also used for optional chaining (opens in a new tab), which essentially makes things optional when working with objects. thats not a ternary operator so dont wory about that too much but might be cool to know
- check this out, the modulo example function above turns into this with a ternary
function isOddOrEven(number) { return number % 2 === 0 ? "Even" : "Odd"; }
-
equality. these you would be able to figure out and use without explaination, but some people don't know the difference between
==
and===
, which can be a little confusing- basically
==
checks equality and===
checks for strict equality which includes data type. this is ammo for JS haters but I think its kind of cool to have that flexability
5 == "5"; // this returns true even though its a number checking against a string 5 === "5"; // returns false bc it is checking for strict equality
- we also have
!=
not equal and the strict version!==
- basically
Logical Operators
- these are simple but very important for writing logic with code
- AND
&&
: returns true if both statements are true. - OR
||
: returns true if one of the statements is true. - NOT
!
: reverses the result, returns false if the result is true.