Advertisement
karlakmkj

Using ternary operator

Dec 8th, 2020 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //first block
  2. let isLocked = false;
  3.  
  4. //using if-else statement
  5. if (isLocked) {
  6.   console.log('You will need a key to open the door.');
  7. } else {
  8.   console.log('You will not need a key to open the door.');
  9. }
  10.  
  11. //using ternary operator to replace if-else statement
  12. isLocked ? console.log('You will need a key to open the door.') : console.log('You will not need a key to open the door.');
  13.  
  14. //second block
  15. let favoritePhrase = 'Love That!';
  16.  
  17. //using if-else statement
  18. if (favoritePhrase === 'Love That!') {
  19.   console.log('I love that!');
  20. } else {
  21.   console.log("I don't love that!");
  22. }
  23.  
  24. //using ternary operator to replace if-else statement
  25. favoritePhrase === 'Love That!' ?  console.log('I love that!') : console.log("I don't love that!");
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement