mary_guzman
mary_guzman 6h ago β€’ 0 views

JavaScript if-else example code for beginners

Hey there! πŸ‘‹ Let's break down JavaScript's `if-else` statements – super important for controlling the flow of your code! I've got a quick guide and a fun quiz to help you nail it. Let's get coding! πŸ’»
πŸ’» Computer Science & Technology

1 Answers

βœ… Best Answer
User Avatar
kevin315 Dec 30, 2025

πŸ“š Quick Study Guide

  • πŸ”‘ `if` Statement: Executes a block of code if a specified condition is true.
  • πŸšͺ `else` Statement: Executes a block of code if the same condition is false.
  • ⛓️ `else if` Statement: Specifies a new condition to test if the first condition is false.
  • βš–οΈ Conditional Expressions: Use comparison operators (e.g., `==`, `!=`, `>`, `<`) to create conditions.
  • 🧱 Code Blocks: Blocks of code within `if`, `else if`, and `else` are enclosed in curly braces `{}`.
  • ✍️ Syntax: javascript if (condition) { // code to execute if condition is true } else if (anotherCondition) { // code to execute if anotherCondition is true } else { // code to execute if all conditions are false }

Practice Quiz

  1. Which keyword is used to specify a block of code to be executed, if a specified condition is true?
    1. A. `when`
    2. B. `if`
    3. C. `condition`
    4. D. `true`
  2. What does the `else` statement do in an `if-else` construct?
    1. A. It specifies a new condition to test.
    2. B. It executes a block of code if the `if` condition is true.
    3. C. It executes a block of code if the `if` condition is false.
    4. D. It always executes, regardless of the `if` condition.
  3. Which of the following is a valid `if-else` statement in JavaScript?
    1. A. `if condition then { // code } else { // code }`
    2. B. `if (condition) { // code } otherwise { // code }`
    3. C. `if (condition) { // code } else { // code }`
    4. D. `condition if { // code } else { // code }`
  4. What is the purpose of the `else if` statement?
    1. A. To end the `if-else` construct.
    2. B. To specify a new condition to test if the preceding `if` condition is false.
    3. C. To execute code regardless of any conditions.
    4. D. To define a default condition.
  5. What will the following code output? javascript let x = 10; if (x > 5) { console.log("Greater than 5"); } else { console.log("Less than or equal to 5"); }
    1. A. "Less than or equal to 5"
    2. B. "Greater than 5"
    3. C. Error
    4. D. Nothing
  6. Which operator is commonly used in the condition part of an `if` statement to check for equality?
    1. A. `=`
    2. B. `==`
    3. C. `===`
    4. D. Both B and C
  7. What happens if the condition in an `if` statement is always false?
    1. A. The `else` block, if present, will execute.
    2. B. The code inside the `if` block will execute.
    3. C. An error will occur.
    4. D. The program will crash.
Click to see Answers
  1. B
  2. C
  3. C
  4. B
  5. B
  6. D
  7. A

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€