Getting Used to Comparison & Logic: Operators to define our codes

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 below for the operators I have learned about thus far: comparison, arithmetic, assignment and logical 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:

equal tox == 8FALSE

x == 5TRUE

equal value and equal typex === “5”

x === 5
!=not equalx != 8TRUE
!==not equal value or not equal typex !== “5”TRUE

x !== 5FALSE
>greater thanx > 8FALSE
<less thanx < 8TRUE
>=greater than or equal tox >= 8FALSE
<=less than or equal tox <= 8TRUE

Arithmetic operators are used to perform arithmetic between variables and/or values.
Given that y = 5, the table below explains the arithmetic operators:
OperatorDescriptionExampleResult in yResult in x
+Additionx = y + 2y = 5x = 7
Subtractionx = y – 2y = 5x = 3
*Multiplicationx = y * 2y = 5x = 10
/Divisionx = y / 2y = 5x = 2.5
%Modulus (division remainder)x = y % 2y = 5x = 1
Incrementx = ++yy = 6x = 6
x = y++y = 6x = 5
Decrementx = –yy = 4x = 4
x = y–y = 4x = 5
Assignment operators are used to assign values to JavaScript variables.
Given that x = 10 and y = 5, the table below explains the assignment operators:
OperatorExampleSame AsResult in x
=x = yx = yx = 5
+=x += yx = x + yx = 15
-=x -= yx = x – yx = 5
*=x *= yx = x * yx = 50
/=x /= yx = x / yx = 2
%=x %= yx = x % yx = 0

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:
&&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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s