Expressions and Operators
Hello HaWkers! Jeff Bruchado here.
This is another article in our series that will cover our journey to Learn Programming from Scratch.
Today will be an article about extremely important content to proceed with your learning.
So please! If you don't have mastery over what I'll talk about today, read to the end!
Well, without further ado, let's get started!
Today, we're going to talk about one of the super important topics in Programming:
Expressions and Operators.
First of all, let's understand why it's essential to understand Expressions and Operators in the Programming world, especially in JavaScript.
Imagine you're working on a project, and you need to create a function that calculates the discount on a purchase.
The discount may vary according to the purchase value, for example:
If the user spends more than 100 dollars, they get a 10% discount.
You must be thinking: "Okay, but how do I make the system understand these conditions?"
And that's where Expressions and Operators come in, my friend!
Expressions and Operators are the heart of your code. They're like the math of programming.
You know that thing about adding, subtracting, comparing things? Well, all of that!
To help you understand, Expressions are like sentences we use to express an idea or ask a question.
In JavaScript, they can be numbers, strings, or anything that gives a value.
However, Operators are the guys that act on these expressions.
They're like the verbs.
There are mathematical operators (+ and -), logical (&& and ||), among others.
For example, when you do age >= 18, you're using an expression (age) and an operator (>=) to check if someone is of legal age.
So, see how important this is:
Expressions: Help create values and give meaning to what you want to do.
Operators: Give you the power to verify, calculate, and make decisions based on these expressions.
In summary, without understanding Expressions and Operators, your code won't have the necessary logic to make decisions and make things happen.
It's like trying to follow a recipe without knowing the ingredients or how to mix them. Not gonna work, right?
Now that you've got the idea, let's dive into this universe and see how these things work in practice!
Assignment Operators
They're used to assign values to variables.
Assignment operators include:
- Simple assignment operator (=)
- Addition assignment operator (+=)
- Multiplication assignment operator (*=).
For example:
let x = 10; // Assigns the value 10 to variable x
x += 5; // Adds 5 to variable x, resulting in x = 15
x *= 2; // Multiplies x by 2, resulting in x = 30
x /= 2; // Divides x by 2, resulting in 15
Comparison Operators
They're used to compare two values and return a boolean value (true or false).
Comparison operators include:
- Equality operator (==)
- Strict equality operator (===)
- Inequality operator (!=)
- Strict inequality operator (!==)
For example:
let x = 10;
let y = '10';
console.log(x == y); // Returns true, because x and y are considered equal
console.log(x === y); // Returns false, because x is a number and y is a string
console.log(x != y); // Returns false, because x and y are considered equal
console.log(x !== y); // Returns true, because x is a number and y is a stringArithmetic Operators
They're used to perform basic mathematical operations.
Arithmetic Operators include:
- Addition operator (+)
- Subtraction operator (-)
- Multiplication operator (*)
- Division operator (/)
For example:
let x = 10;
let y = 5;
console.log(x + y); // Returns 15
console.log(x - y); // Returns 5
console.log(x * y); // Returns 50
console.log(x / y); // Returns 2Bitwise Operators
They're used to perform bitwise operations on integers.
Bitwise Operators include:
- AND operator (&)
- OR operator (|)
- XOR operator (^)
AND (&)
The AND operator (&) performs a bitwise AND operation between the operands.
This means that, for each bit, if both operands have the bit set to 1, the result will have that bit set to 1.
Otherwise, the result will have that bit set to 0.
For example:
let x = 12; // 1100 in binary
let y = 25; // 11001 in binary
console.log(x & y); // Returns 8 (1000 in binary)OR (|)
The OR operator (|) performs a bitwise OR operation between the operands.
This means that, for each bit, if either operand has the bit set to 1, the result will have that bit set to 1.
Otherwise, the result will have that bit set to 0.
For example:
let x = 12; // 1100 in binary
let y = 25; // 11001 in binary
console.log(x | y); // Returns 29 (11101 in binary)XOR (^)
The XOR operator (^) performs a bitwise XOR operation between the operands.
This means that, for each bit, if only one of the operands has the bit set to 1, the result will have that bit set to 1.
Otherwise, the result will have that bit set to 0.
For example:
let x = 12; // 1100 in binary
let y = 25; // 11001 in binary
console.log(x ^ y); // Returns 21 (10101 in binary)Conclusion about Bitwise Operators
Bitwise operators are often used to perform mask operations on integers.
Where some bits are used to store information and others are ignored.
They're also useful for performing bit shift operations, where the bits of a number are shifted left or right.
Logical Operators
They're used to perform logical operations.
They're the operators that deal with the truths and lies of code hahaha.
Logical Operators include:
- AND operator (&&)
- OR operator (||)
- NOT operator (!)
❗ YOU NEED TO UNDERSTAND THESE OPERATORS, BECAUSE BY FAR THEY'RE THE MOST USED!
For example:
let x = true;
let y = false;
console.log(x && y); // Returns false
console.log(x || y); // Returns true
console.log(!x); // Returns falseBigInt Operator
It's a special operator used to work with BigInt values, which are integers of arbitrary size.
❗ I CONFESS THAT THIS BIGINT OPERATOR ISN'T MUCH USED IN DAY-TO-DAY
For example:
let x = BigInt(10);
let y = BigInt(5);
console.log(x + y); // Returns 15n (n is used to indicate that the value is a BigInt)
console.log(x - y); // Returns 5nString Operators
They're used to concatenate (join) two or more strings.
For example:
let x = 'Hello, ';
let y = 'world!';
console.log(x + y); // Returns "Hello, world!"In the example above, there's a better way to do this approach, which is using Template literals (Template strings), but I'll cover this topic in another article later.
Conditional Operators
They're used to perform conditional operations, like the ternary operator (?:).
The ternary operator is used to determine which value should be assigned to a variable based on a condition.
For example:
let x = 10;
let y = x > 5 ? 'greater than 5' : 'less than or equal to 5';
console.log(y); // Returns "greater than 5"The Comma Operator
It's used to separate expressions in a statement.
It evaluates each expression from left to right and returns the value of the last expression evaluated.
For example:
let x = (10, 20, 30);
console.log(x); // Returns 30Unary Operators
They're operators that operate on only one operand.
Unary Operators include the negation operator (-), the increment operator (++), and the decrement operator (--).
For example:
let x = 10;
console.log(-x); // Returns -10
x++; // Increments x by 1, resulting in x = 11
console.log(x); // Returns 11
x--; // Decrements x by 1, resulting in x = 10
console.log(x); // Returns 10
Relational Operators
They're used to compare two values and return a boolean value (true or false).
They are similar to comparison operators, but relational operators return a boolean value based on the relationship between the operands, while comparison operators return a boolean value based on the equality between the operands.
Relational operators include the following operators:
- Greater than operator (>)
- Less than operator (<)
- Greater than or equal to operator (>=)
For example:
let x = 10;
let y = 5;
console.log(x > y); // Returns true
console.log(x < y); // Returns false
console.log(x >= y); // Returns trueConclusion
In summary, expressions and operators are fundamental to any programming language.
They allow us to perform various operations, such as assigning values to variables, comparison, mathematical and logical operations, string concatenation, and conditional operations.
It's important to have knowledge about the different types of operators available, even if you don't need to use all of them very frequently.
This will allow you to have an overview of what's possible with these operators and you can refer to them when you need to perform a specific operation in your code.
Well HaWker, we've reached the end of another article.
I hope this article has added to your repertoire of programming knowledge.
As always, if you have any questions or want to explore any point further, just reply with your question to this email.
And remember, I'm here to help you improve 1% with each new article!
If you made it this far, as usual, please reply to this email with "I managed to learn about the different expressions and operators".
This way I'll know that this article series is being useful for you, and that you're following along and taking advantage.
As you already know! Only a true HaWker makes it to the end.
Congratulations!
I'm happy to have you here.
See you soon.
Let's go! 🦅
- Jeff Bruchado
Congratulations on making it this far, HaWker! 🎉
Now that you've learned about expressions and operators in programming, how about testing your knowledge? Take the quiz below and see how much you absorbed from our article.
It's a great way to reinforce learning and prepare for the next steps in your programming journey.
Good luck and have fun!

