GithubHelp home page GithubHelp logo

js-nested-conditions-and-operators's Introduction

Writing Nested Conditional Statements and Using Logical Operators in JavaScript

Nested Conditions

  • Nested conditional statements in JavaScript refer to the practice of placing one conditional statement inside another conditional statement. This allows you to create more complex decision-making logic based on multiple conditions. The inner conditional statements are often enclosed within the blocks of the outer conditional statements.
if (condition1) {
  // Code block executed if condition1 is true
  if (condition2) {
    // Code block executed if condition1 and condition2 are true
    // Nested conditional statements can continue further if needed
  } else {
    // Code block executed if condition1 is true, but condition2 is false
  }
} else {
  // Code block executed if condition1 is false
}

Else if Statements

  • In JavaScript, else if statements are used to add additional conditions to the decision-making process. They allow you to specify alternative conditions to be checked when the initial if statement condition is false. The else if statement is an extension of the if statement and provides a way to chain multiple conditions together.
if (condition1) {
  // Code block executed if condition1 is true
} else if (condition2) {
  // Code block executed if condition2 is true (and condition1 is false)
} else if (condition3) {
  // Code block executed if condition3 is true (and both condition1 and condition2 are false)
} else {
  // Code block executed if none of the conditions are true
}

Logical Operators

  • In JavaScript, logical operators are used to perform logical operations on Boolean values (true or false) and to combine or modify the result of Boolean expressions. There are three main logical operators in JavaScript:
  • The logical AND operator (&&) returns true if both condition are true; otherwise, it returns false.
let x = 5;
let y = 10;

console.log(x < 10 && y > 5); // Output: true (both conditions are true)
console.log(x < 3 && y > 15); // Output: false (second condition is false)
  • The logical OR operator (||) returns true if at least one of the operands is true; otherwise, it returns false.
let a = 2;
let b = 8;

console.log(a === 2 || b === 10); // Output: true (first condition is true)
console.log(a === 3 || b === 10); // Output: false (both conditions are false)
  • The logical NOT operator (!) negates the Boolean value of an operand. It converts true to false and false to true.
let isLogged = false;

console.log(!isLogged); // Output: true (negates the false value to true)

Prep

  1. Connect your js file to your HTML file
  2. Open your code in the browser and open your console

Activities

  1. In the js file, complete the list of exercises.

js-nested-conditions-and-operators's People

Contributors

abbreviatedman avatar lhack47 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.