DEV Community

Jennifer Tieu
Jennifer Tieu

Posted on • Updated on

Self-Taught Developer Journal, Day 28: TOP JavaScript Fundamentals Part 2 - Logical Operators and Conditionals

Today I learned...

The || OR operator

  • represented by two vertical lines
  • if any of the values are true, it will return true
  • if the operand is not boolean, it is converted to a boolean for evaluation
  • returns the first true value with the original value of the operand, does not continue to evaluate operands in the expression (also known as "short circuit evaluation")
  • return the last false value if all operands evaluated and are false *A value is returned in its original form, not the conversion. In a chain of || OR, the first truthy value is returned or the last one if no truthy value is found.

The && AND operator

  • represented by two ampersands &&
  • returns true if both operands are true and false otherwise
  • returns the first falsy value
  • returns the last value if all values are true
  • && (AND) has a higher precedence then || (OR)

The ! NOT operator

  • represented with an exclamation !
  • converts value to boolean then returns the inverse value
  • higher precedence then && (AND) and || (OR)
  • !! can be used to convert value to a boolean

Switch Statement

An alternative to the if..else statements when checking for matching values of a single expression\value input.

const color = 'blue';

switch (color){ // expression or value
case 'red':
console.log('Color is red');
break;
case 'blue': // choice matches the expression/value 'blue'
console.log('Color is blue');
break; // if the previous choice matches, stop executing the code block or it will continue to the next choice
case 'green':
console.log('Color is green');
break;
default: // will run if none of the choice matches, do not need a break statement because there are not more statements after to execute
console.log('No matches');
}

Enter fullscreen mode Exit fullscreen mode




Resources

The Odin Project
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/conditionals

Top comments (0)