Lecture 2 - Operators & Conditional Statements
Lecture 2 - Operators & Conditional Statements
In web development, we use conditional statements (like if) to make decisions based on
true or false conditions. Operators (like ==, >, <, +, % ) are used inside these statements
to perform some operations on the operands like comparison, calculation, or logic checking
etc.
Conditional statements: Decide actions based on conditions (e.g., "If it is raining,
take an umbrella").
Operators: Perform operations like arithmetic (+, -), comparisons (==, >, <), or
logical checks (&&, ||).
Together, they help implement real-life conditions in websites.
How to comment in JS
A comment is a part of the code that is not executed by JavaScript. It is typically used to
explain the code, make notes, or clarify the functionality of the code for anyone reading the code.
To comment out a line or block of code in JavaScript, you can use Ctrl + / in many code editors."
Additional Notes:
Single-line comments in JavaScript start with //.
Multi-line comments are enclosed in /* */.
The shortcut Ctrl + / is a feature of most code editors (e.g., VS Code, Sublime Text) and not
part of JavaScript itself.
Operators in JavaScript.
Operators in JavaScript are symbols or keywords used to perform operations on values
or variables. They help us manipulate data.
Examples:
let a = 5;
let b = 2;
console.log(“a + b”) = 5 + 2 = 7 // Addition
console.log(“a - b”) = 5 - 2 = 3 // Subtraction
console.log(“a * b”) = 5 * 2 = 10 // Multiplication
console.log(“a / b”) = 5 / 2 = 2.5 // Division
console.log(“a % b”) =5%2=1 // Modulus
console.log(“a ** b”) = 5 ** 2 = 25 // Exponentiation means 5 to the power
(5^2)
Unary Operators
In JavaScript, increment and decrement operators are used to increase or decrease
a value by 1.
1. Increment Operator (++):
o Increases a value by 1.
o Example:
let x = 5;
x++; // Now x is 6
2. Decrement Operator (--):
o Decreases a value by 1.
o Example:
let y = 5;
y--; // Now y is 4
Types of Increment and Decrements:
Post-increment/decrement:
The operator comes after the variable (x++ or x--). The value is used first,
then changed.
Pre-increment/decrement:
The operator comes before the variable (++x or --x). The value is changed
first, then used.
o Example:
let a = 5;
console.log(a++); // Prints 5, then a becomes 6
console.log(++a); // Increases a to 6, then prints 6
How Pre- and Post-Increment/Decrement Work:
Pre-decrement (--x): The value of the variable is decreased first, and then the
new value is used in the calculation or condition.
o Think of it as "decrease now, then use."
Post-decrement (x--): The current value is used first, and then the variable is
decreased afterward.
o It's like "use the value, then decrease."
Pre-increment (++x): The value of the variable is increased first, and then the
new value is used in the calculation or condition.
o "Increase now, then use."
Post-increment (x++): The current value is used first, and then the variable is
increased afterward.
o "Use the value, then increase."
Summary:
Pre changes the value first, then uses it.
Post uses the value first, then changes it.
Here are simple coding examples for each type of increment and decrement, with
comments to explain what’s happening:
Post-Decrement (x--)
let x = 5;
let result = x--; // result is assigned 5, then x is decreased to 4
console.log(result); // Outputs 5
Pre-Increment (++x)
let x = 5;
let result = ++x; // x is increased to 6, then result is assigned 6
console.log(result); // Outputs 6
Post-Increment (x++)
let x = 5;
let result = x++; // result is assigned 5, then x is increased to 6
console.log(result); // Outputs 5
_______________________________________________________________________________
______
The unary operators (a++ and a--) increase or decrease the value of a by 1.
o a++ is the same as a = a + 1.
o a-- is the same as a = a - 1.
The assignment operators (a += 4 and a -= 4) change the value of a by a
specific number.
o a += 4 is the same as a = a + 4.
o a -= 4 is the same as a = a - 4.
In short, ++ or -- changes a by 1, while += or -= changes a by any number you
specify.
_______________________________________________________________________________
______
Expression in JS ( “a + b” )
The whole part "a + b" is called an expression, where "a" and "b" are called operands, and
the "+" sign between them is called the operator.
An operator performs a specific action on values or variables, such as addition,
comparison, or assignment, to produce a result or change a value.
Conditional statements let your program take different actions based on certain
conditions. Examples include if, else if, else, and switch.
if Statement
Purpose: Execute a block of code if a condition is true.
Example: Check if the temperature is high.
2. if...else Statement
Purpose: Execute one block of code if the condition is true and another if it's false.
Example: Check if a score is passing.
Age Eligibility
Example: Check multiple age conditions and respond accordingly.
let age = 16;
let day = 2;
switch (day) {
case 1:
console.log("Monday");
break;
case 2:
console.log("Tuesday");
break;
case 3:
console.log("Wednesday");
break;
default:
console.log("Another day");
}
Voting Eligibility
Example: Check if a person is eligible to vote.
switch (true) {
case (age >= 18):
console.log("You can vote.");
break;
default:
console.log("You cannot vote.");
}
These examples illustrate how to use conditional statements effectively in JavaScript for
various scenarios.
Ternary Operator
The ternary operator is a short way to write an if-else statement in one line. It uses a
question mark (?) and is often used to choose between two values.
How It Works
Example
let age = 16;
let canVote = (age >= 18) ? "Yes, you can vote." : "No, you cannot vote.";
In this example, it checks if age is 18 or older. If true, it says "Yes, you can vote." If false, it
says "No, you cannot vote."
____________________________________________________________________________________________
_______