if(statements).....as I understand them

Introduction

What do you mean “if”?

When writing a loop, you might think, "I wish there was a way to do something as long as something else is true." If you know what I’m talking about, then in this post, I will try my best to explain “if” statements as I understand them.

Understanding the "if...else" Statement

The "if...else" or "if statement" executes a statement if a described condition is true. If the condition is false, however, you can also write an "else" statement that could be executed if different conditions are met. There is no limit to the number of "else"s you can add, as far as I’ve found.

Let's see an example:

function shouldICook(appetite) {
  let decision;
  if (appetite === 'very hungry') {
    decision = 'Order out.';
  } else if(appetite === 'kinda hungry') {
    decision = 'Cook with what you have.';
  } else if(appetite === 'not hungry') {
    decision = 'Go to the Store.'  
  } else {
    return 'How hungry are you? very hungry, kinda hungry, or not hungry?'
  }
  console.log(decision)
}

shouldICook('very hungry')

Now let’s break it down:

function shouldICook(appetite) {
  // Declare a variable 'decision' without assigning a value yet.
  let decision;

  // Check the value of 'appetite' and decide based on its value.
  if (appetite === 'very hungry') {
    // If appetite is 'very hungry', set the decision to 'Order out.'
    decision = 'Order out.';
  } else if (appetite === 'kinda hungry') {
    // If appetite is 'kinda hungry', set the decision to 'Cook with what you have.'
    decision = 'Cook with what you have.';
  } else if (appetite === 'not hungry') {
    // If appetite is 'not hungry', set the decision to 'Go to the Store.'
    decision = 'Go to the Store.';
  } else {
    // If none of the above conditions match, return a default message.
    return 'How hungry are you? very hungry, kinda hungry, or not hungry?';
  }

  // Log the decision to the console.
  console.log(decision);
}

// Call the function with 'very hungry' as the argument.
shouldICook('very hungry');
// This will print: "Order out."

Basics of If Statements

Definition and purpose

"If" statements let you evaluate a condition and execute code based on whether the condition is true.

Syntax and structure

Here’s how a simple "if" statement looks:

let x = 10;

if (x > 5) {
    console.log("x is greater than 5");
}

This simple "if" statement checks the value of number. If the condition is true, it runs the console.log.

If-else statement

Here’s how a simple "if-else" statement looks:

let number = 10;

if (number > 5) {
    console.log("The number is greater than 5.");
} else {
    console.log("The number is 5 or less.");
}

This "if-else" statement checks the value of number. If the condition is true, it logs a message like the previous example. However, if number is less than or equal to 5, it will now log: “The number is 5 or less.”

Example of a basic if statement

Here’s a pretty standard use of an if statement:

function checkNumber(number) {
  // Check if the number is greater than 0
  if (number > 0) {
    console.log("The number is positive.");
  } 
  // Check if the number is less than 0
  else if (number < 0) {
    console.log("The number is negative.");
  } 
  // If the number is neither greater or less than 0
  else {
    console.log("The number is zero or not an integer.");
  }
}

This "if" statement takes in number as the others did.

  • If number is greater than 0, it logs: “The number is positive.”

  • If number is less than 0, it logs: “The number is negative.”

  • If number is anything else (like 0 or not a number), it logs: “The number is zero or not an integer.”

Common Pitfalls and Errors

Logical errors

Example: Using = instead of === in your conditions.

  • Incorrect: if (appetite = ‘very hungry’)

  • Correct: if (appetite === 'very hungry')

Syntax errors

Forgetting brackets {} or parentheses () in your "if" statements can lead to errors.

Best practices to avoid mistakes

  • Always wrap your conditions in () to ensure clarity.

  • Always use === or !== for accurate comparisons.

  • Pay close attention to your nesting to keep your code readable and organized.

Conclusion

"If" statements are a fundamental part of programming, allowing you to make decisions in your code. Once you get comfortable with their syntax and steer clear of common mistakes, they become quite easy to use. Experiment with different conditions, and I hope this helps you understand "if" statements, at least as I understand them.