Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- CONTROL FLOW: IF, ELSE-IF, ELSE
- Conditionals are great to use with booleans.
- */
- var a = 1;
- if(a > 0) {
- print('Yes');
- } if (a === 1) {
- print('Yes, too');
- }
- function print(value) { // another way of checking the result
- console.log(value); // prints both 'Yes' and 'Yes, too'.
- }
- /*
- That was quite a redundant example, you really don't want to do this.
- I wanted to show that if you use "if' several times, it's not really the best way to do it.
- Each time you are checking the same condition.
- In this case else if prevents to check the same condition if it already worked once.
- */
- var a = 1;
- if(a > 0) {
- print('Yes');
- } else if (a === 1) {
- print('Yes, too');
- }
- function print(value) {
- console.log(value); // prints only 'Yes'
- }
- var a = 1;
- if(a < 0) {
- print('Yes');
- } else if (a === 0) {
- print('Yes, too');
- } else {
- print('Liar');
- }
- function print(value) {
- console.log(value); // prints 'Liar' because first two conditions weren't correct
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement