JavaScript Lesson 3

  • If statement checks a condition and will execute a task if the condition evaluates to true.
    • if (isMailSent) { console.log(‘Mail sent to recipient’};
  • If….Else statements make binary decisions and execute different code blocks based on a provided condition.
    • We can add more conditions using else if statements.if (isTaskCompleted){console.log(‘Task Completed’);} else{console.log(‘Task Incomplete’);}
  • Comparison operators, include <, >, <=, >=, ===, and != can compare two values.
  • The logical and operator &&, or “and”, checks if both provided expressions are truthy.
  • The logical operator ||, or “or”, checks if either provided expressions is truthy.
  • The bang operator ! switches the truthiness and falsiness of a value.
  • The ternary operator is shorthand to simplify concise if… else statements.
    • isNightTime ? console.log(‘Lights on!’) : console.log(‘Lights off!’);
  • A switch statement can be used to simplify the process of writing multiple else if statements. The break keyword stops the remaining cases from being checked and executed in a switch statement.
    • switch(food){ case ‘oyster’ : console.log(‘Enjoy the taste of the sea’); break; case ‘pizza’ : console.log (‘Enjoy a delicious pie’); break; default: console.log(‘Enjoy your meal’);}
  • https://www.codecademy.com/learn/introduction-to-javascript/modules/learn-javascript-control-flow/reference

Leave a comment