Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. /*
  2. CONTROL FLOW: IF, ELSE-IF, ELSE
  3.  
  4. Conditionals are great to use with booleans.
  5. */
  6.  
  7. var a = 1;
  8. if(a > 0) {
  9. print('Yes');
  10. } if (a === 1) {
  11. print('Yes, too');
  12. }
  13. function print(value) { // another way of checking the result
  14. console.log(value); // prints both 'Yes' and 'Yes, too'.
  15. }
  16.  
  17. /*
  18. That was quite a redundant example, you really don't want to do this.
  19. I wanted to show that if you use "if' several times, it's not really the best way to do it.
  20. Each time you are checking the same condition.
  21. In this case else if prevents to check the same condition if it already worked once.
  22. */
  23.  
  24. var a = 1;
  25. if(a > 0) {
  26. print('Yes');
  27. } else if (a === 1) {
  28. print('Yes, too');
  29. }
  30.  
  31. function print(value) {
  32. console.log(value); // prints only 'Yes'
  33. }
  34.  
  35. var a = 1;
  36. if(a < 0) {
  37. print('Yes');
  38. } else if (a === 0) {
  39. print('Yes, too');
  40. } else {
  41. print('Liar');
  42. }
  43. function print(value) {
  44. console.log(value); // prints 'Liar' because first two conditions weren't correct
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement