One of the challenges coming into programming as someone without a degree in math, is the notion that you can still code without having crazy math skills. While this is true, the super logical nature of code can take some getting used to, especially if the last time you really did Algebra was when you were a teenager and are still learning the syntax of code as well. With code, how data is manipulated lies heavily on knowing how to work with large collection of small operations that perform simple mathematical functions strategically to help us achieve computing capabilities with our Javascript.
Because the nature of code is of bridging many collections of many small operations, one’s code could end up being quite complex and intimidating. I wanted to get better at understanding the various operators that are used out there through Javascript and other programming languages before I started getting more into writing code. I’m including some helpful charts from w3schools.org below for the operators I have learned about thus far: comparison, arithmetic, assignment and logical operators.
COMPARISON OPERATORS | |||
---|---|---|---|
Comparison operators are used in logical statements to determine equality or difference between variables or values. | |||
Given that x = 5, the table below explains the comparison operators: | |||
Operator | Description | Comparing | Returns |
== | equal to | x == 8 | FALSE |
x == 5 | TRUE | ||
=== | equal value and equal type | x === “5” | FALSE |
x === 5 | TRUE | ||
!= | not equal | x != 8 | TRUE |
!== | not equal value or not equal type | x !== “5” | TRUE |
x !== 5 | FALSE | ||
> | greater than | x > 8 | FALSE |
< | less than | x < 8 | TRUE |
>= | greater than or equal to | x >= 8 | FALSE |
<= | less than or equal to | x <= 8 | TRUE |
ARITHMETIC OPERATORS | ||||
---|---|---|---|---|
Arithmetic operators are used to perform arithmetic between variables and/or values. | ||||
Given that y = 5, the table below explains the arithmetic operators: | ||||
Operator | Description | Example | Result in y | Result in x |
+ | Addition | x = y + 2 | y = 5 | x = 7 |
– | Subtraction | x = y – 2 | y = 5 | x = 3 |
* | Multiplication | x = y * 2 | y = 5 | x = 10 |
/ | Division | x = y / 2 | y = 5 | x = 2.5 |
% | Modulus (division remainder) | x = y % 2 | y = 5 | x = 1 |
++ | Increment | x = ++y | y = 6 | x = 6 |
x = y++ | y = 6 | x = 5 | ||
— | Decrement | x = –y | y = 4 | x = 4 |
x = y– | y = 4 | x = 5 |
ASSIGNMENT OPERATORS | |||
---|---|---|---|
Assignment operators are used to assign values to JavaScript variables. | |||
Given that x = 10 and y = 5, the table below explains the assignment operators: | |||
Operator | Example | Same As | Result in x |
= | x = y | x = y | x = 5 |
+= | x += y | x = x + y | x = 15 |
-= | x -= y | x = x – y | x = 5 |
*= | x *= y | x = x * y | x = 50 |
/= | x /= y | x = x / y | x = 2 |
%= | x %= y | x = x % y | x = 0 |
LOGICAL OPERATORS | ||
---|---|---|
Logical operators are used to determine the logic between variables or values. | ||
Given that x = 6 and y = 3, the table below explains the logical operators: | ||
Operator | Description | Example |
&& | and | (x < 10 && y > 1) is true |
|| | or | (x === 5 || y === 5) is false |
! | not | !(x === y) is true |
Often times, it is possible that shorthand codes exist for many of these operators, and it’s a good idea to keep a chart on hand of both the regular and shorthand operators so one is not spending extra time just trying to decode what the operators are doing to the operands in your code. In addition to knowing about the shorthand and eventually using them commonly enough to have them memorized, there is an emphasis in the etiquette of writing clean code and one has to be cautious with using too many shorthand operators–these could end up looking like hieroglyphics and make the code hard to read. I’ve read now in several resources studying operators that this principal is important and it is super important to be concise with them.
This great article over at freecodecamp goes into detail about the circumstances surrounding operators (the author calls it Syntactic Sugar) and explains some of the cool shorthand operations I’ve yet to learn about but keep seeing pop up time and again like the ternary operator. Perhaps I will experiment with using some in my codes later on throughout the journey of the course and now have a good resource to reference when I’m thinking of how I can simplify my code. For now, I will get as used to the language of operators as best as I can.