The journey continues, and I’m loving every step! Today, I delved into Basic Operators (Arithmetic, Assignment, Logical, Comparison) and Control Structures (if/else, switch, loops). These concepts are foundational, yet so powerful for building logic in code. Here’s a quick recap of what I learned:
Operators
1. Arithmetic Operators
These are the building blocks for mathematical operations:
+
Addition
-
Subtraction
/
Division
*
Multiplication
**
Exponentiation (used to find powers
, e.g., 2 ** 3 = 8)
%
Modulus (finds the remainder of a division, e.g., 5 % 2 = 1)
2. Assignment Operators
Used to assign or update values in a variable:
=
Assigns a value. Example: a = 5
+=
, -=
, *=
, /=
, %=
These combine arithmetic and assignment.Example:
let a = 7;
a += 3; // a = a + 3
console.log(a); // Output: 10
**=
Raises the variable to a power and assigns the result.
3. Comparison Operators
These compare two values and return a Boolean (true or false):
==
Equal to: Compares values but not types.
!=
Not equal: True if values are different.
===
Strict equal: Compares both value and type.
!==
Strict not equal: Checks that both value and type are different.
>
, <
, >=
, <=
Greater than, less than, and their equals variants.
4. Logical Operators
Used to combine or manipulate Boolean values:
&&
(Logical AND): Returns true only if all conditions are true
.
||
(Logical OR): Returns true
if any condition is true
.
!
(Logical NOT): Flips the Boolean value (!true
becomes false
).
Control Structures
1. if/else Statements
These are conditional blocks that execute code only when a condition is met. Example:
let age = 18;
if (age >= 18) {
console.log('You are an adult.');
} else {
console.log('You are a minor.');
}
2. Switch Statements
A cleaner alternative to multiple if/else blocks, often used for checking one value against multiple cases. Example:
let day = 'Monday';
switch (day) {
case 'Monday':
console.log('Start of the work week.');
break;
case 'Friday':
console.log('Almost the weekend!');
break;
default:
console.log('It’s just another day.');
}
3. Loops
Loops are used to execute a block of code repeatedly:
For Loop:
Iterates a set number of times.While Loop
: Continues until a condition is false.Do-While Loop
: Executes at least once before checking the condition.
Final Thoughts
Today was packed with knowledge! These concepts might seem basic, but they form the backbone of any programming language.
Slow and steady wins the race, and I’m taking it one step at a time. Day 3 is already on my mind, and I can't wait to see what’s next.
Want to join me on this journey? Let’s learn together!
Stay tuned for Day 3!
Top comments (0)