Science Knowings: JavaScript Course For Social Media

Conditional Statements (if, else if, else)

Objects and Object Methods Recap

In the last session, we explored objects and their methods in JavaScript. Objects allow us to organize data in a structured way, using key-value pairs.

Purpose of Conditional Statements

Conditional Statements: Making Decisions in JavaScript

Conditional statements, like if-else statements, enable us to control the flow of our programs based on specific conditions. They're like traffic lights, guiding our code to execute different paths depending on the circumstances.

if-else Statement Syntax

if (condition) {
 // code to be executed if condition is true
} else {
 // code to be executed if condition is false
}

if-else Example

Let's check if a number is positive or not:

if (number > 0) {
 console.log("The number is positive.")
} else {
 console.log("The number is non-positive.")
}

Nested if Statements

Sometimes, we need more complex conditions:

if (condition1) {
 if (condition2) {
  // code to be executed if both conditions are true
 } else {
  // code to be executed if condition1 is true and condition2 is false
 }
}

else if Statement Syntax

if (condition1) {
 // code to be executed if condition1 is true
} else if (condition2) {
 // code to be executed if condition1 is false and condition2 is true
}

else if Statement Example

Let's grade a student based on their score:

if (score >= 90) {
 console.log("A")
} else if (score >= 80) {
 console.log("B")
} else {
 console.log("C or below")
}

Chaining else if Statements

You can chain multiple else if statements for more complex conditions.

if (condition1) {
 // code to be executed if condition1 is true
} else if (condition2) {
 // code to be executed if condition1 is false and condition2 is true
} else if (condition3) {
 // code to be executed if condition1 and condition2 are false and condition3 is true
}

else Statement Syntax

if (condition) {
 // code to be executed if condition is true
} else {
 // code to be executed if condition is false
}

else Statement Example

Let's check if a user is logged in:

if (isLoggedIn) {
 console.log("Welcome, user!")
} else {
 console.log("Please log in to continue.")
}

Logical Operators for Conditions

Logical operators combine conditions using AND (&&), OR (||), and NOT (!).

OperatorDescription
&& (AND)Returns true if both conditions are true, false otherwise
|| (OR)Returns true if either condition is true, false if both are false
! (NOT)Reverses the logical value of a condition

&& (AND) Operator

Both conditions must be true for the result to be true:

if (age >= 18 && hasDrivingLicense) {
 console.log("Eligible to drive.")
}

|| (OR) Operator

Either condition can be true for the result to be true:

if (isAdmin || isModerator) {
 console.log("Access granted.")
}

! (NOT) Operator

Reverses the logical value:

if (!isWeekend) {
 console.log("It's a weekday.")
}

Conditional (Ternary) Operator

A concise alternative to if-else statements

Conditional Operator Syntax

condition ? trueValue : falseValue

Conditional Operator Example

Let's set a default value for a variable:

const name = firstName ? firstName : "Guest"

Switch Statements (Next Topic)

Switch statements provide an alternative way to evaluate multiple conditions, especially when comparing a variable to several fixed values.