Conditionals in Javascript

Using Codecademy, I learned if/else statements, nested if/else statements, switch statements, and ternary operators in Javascript!

If/Else Statements:


var topThree = "true";
var winner = "true";
var result;

if (topThree === "false") {
   result = "Sorry, empty handed";
}
else {
   if (winner === "true") {
      result = "Gold!!!";
   } else {
      result = "Not bad! Got a medal!";
   }
}

==> Gold!!! 

Switch Statements:


var jacketColor = "green";
var result;

switch (jacketColor) {
    
  case "black":
    result = "Pay $300";
    break;
    
  case "brown":
    result = "Pay $200";  
    break;
    
  case "green":
    result = "Pay $5";
    break;
    
  default:
    result = "This color does not match my eyes!";

}

==> Pay $5

Ternary Operators:


var x = 5;
var y = 4;

result = x > y ? "good job" : 20;

==> good job
This entry was posted in Computing. Bookmark the permalink.

Leave a reply