;

Learn IF Else and Else IF in Basic Javascript

IF Else Introduction in Javascript Basic



IF Else Example Code in Javascript Basics

buayaberdiri.blogspot.com - In programming, conditional statements are used to make decisions based on certain conditions. In JavaScript, the if-else statement is one of the most fundamental and widely used conditional statements. The if-else statement evaluates a condition and executes one block of code if the condition is true, and another block of code if the condition is false. In this article, we will take an in-depth look at the if-else statement in JavaScript.

Syntax of the if-else Statement


The basic syntax of the if-else statement in JavaScript is as follows:


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


The condition in the if statement is a boolean expression that is evaluated to either true or false. If the condition is true, the code block inside the if statement is executed. If the condition is false, the code block inside the else statement is executed.

Here is an example:


let age = 18;
if (age >= 18) {
  console.log("You are an Big.");
} else {
  console.log("You are not an Small yet.");
}


In this example, the age variable is assigned the value of 18. The if statement checks whether age is greater than or equal to 18. Since age is equal to 18, the condition is true, and the code inside the if statement is executed. The output of this code is You are an adult.

Nested if-else Statements


You can also use nested if-else statements in JavaScript. A nested if-else statement is a statement inside another if or else statement. Here is an example:


let grade = 70;
if (grade >= 90) {
  console.log("A");
} else {
  if (grade >= 80) {
    console.log("B");
  } else {
    if (grade >= 70) {
      console.log("C");
    } else {
      console.log("F");
    }
  }
}


In this example, the grade variable is assigned the value of 70. The first if statement checks whether grade is greater than or equal to 90. Since grade is less than 90, the else statement is executed. The second if statement checks whether grade is greater than or equal to 80. Since grade is less than 80, the else statement is executed again. The third if statement checks whether grade is greater than or equal to 70. Since grade is equal to 70, the code inside this if statement is executed, and the output of this code is C.

While nested if-else statements can be useful in certain situations, they can also make your code difficult to read and understand. To avoid this, you can use the switch statement instead.



Else IF Introduction in Javascript Basic


In programming, conditional statements are used to make decisions based on certain conditions. In JavaScript, the if-else statement is one of the most fundamental and widely used conditional statements. However, in some cases, we need to test for more than two possible outcomes. This is where the else if statement comes in. In this article, we will take an in-depth look at the else if statement in JavaScript.

Syntax of the else if Statement


The else if statement is an extension of the if-else statement. It allows you to check for multiple conditions and execute different code blocks based on each condition. The basic syntax of the else if statement in JavaScript is as follows:


if (condition1) {
  // code to be executed if condition1 is true
} else if (condition2) {
  // code to be executed if condition2 is true
} else {
  // code to be executed if neither condition1 nor condition2 is true
}


The else if statement allows you to test for multiple conditions. If the first condition is false, it moves on to the next condition and so on until a condition is found to be true. If none of the conditions are true, the code block inside the else statement is executed.

Here is an example:


let grade = 80;
if (grade >= 90) {
  console.log("A");
} else if (grade >= 80) {
  console.log("B");
} else if (grade >= 70) {
  console.log("C");
} else {
  console.log("F");
}


In this example, the grade variable is assigned the value of 80. The first if statement checks whether grade is greater than or equal to 90. Since grade is less than 90, the else if statement is executed. The second else if statement checks whether grade is greater than or equal to 80. Since grade is equal to 80, the code inside this else if statement is executed, and the output of this code is B.

Chaining Multiple else if Statements


You can chain multiple else if statements to check for more conditions. Here is an example:


let day = "Saturday";
if (day === "Monday") {
  console.log("Today is Monday");
} else if (day === "Tuesday") {
  console.log("Today is Tuesday");
} else if (day === "Wednesday") {
  console.log("Today is Wednesday");
} else if (day === "Thursday") {
  console.log("Today is Thursday");
} else if (day === "Friday") {
  console.log("Today is Friday");
} else if (day === "Saturday") {
  console.log("Today is Saturday");
} else {
  console.log("Today is Sunday");
}

In this example, the day variable is assigned the value of Saturday. The code block inside the else if statement that matches the value of day is executed, and the output of this code is Today is Saturday.



The switch Statement


The switch statement is another way to implement conditional logic in JavaScript. The switch statement evaluates an expression and compares it to multiple cases. If the expression matches a case, the code inside that case is executed. Here is the syntax of the switch statement:


switch (expression) {
  case value1:
    // code to be executed if expression matches value1
    break;
  case value2:
    // code to be executed if expression matches value2
    break;
  default:
    // code to be executed if expression doesn't match any value
    break;
}

In the switch statement, the expression is evaluated, and the code inside the case that matches the expression is executed. If the expression



See the article about javascript here:

  1. Learn Array Method - forEach, map, reduce, filter
  2. Learn Basics Array In Javascript
  3. Learn Basics Asynchronous In Javascript
  4. How To Change String Value Using Replace in Javascript
  5. How to Learn a Calculator Basic in Javascript
  6. What Are Switch in Javascript And How to Use Them
  7. Create a Calculator Using the Python Flask Framework
  8. Basic Learning to Make a Calculator With PHP and HTML
  9. How to Use a FOR Loop in Javascript
  10. Introduction Objects Basics in Javascript



Check out other articles about HTML & CSS:

  1. Learn Input Types In HTML For Login And Register Form
  2. Learn HTML List - How To Create Square, Circle, Number List
  3. How To Create Comments Line In HTML
  4. How to Add Text Formatting In HTML
  5. Adding The Fieldset Code To The Form In HTML
  6. How to Create a Search Box in Pure HTML
  7. Create Color Charts With Pure HTML
  8. How to change font size using CSS
  9. Types of Border Styles in CSS Code
  10. How to Change the Background Color in CSS




List of Article Posts https://buayaberdiri.blogspot.com