Created
May 15, 2023 16:30
-
-
Save suhailgupta03/0cd5ce0cb1c8271c61e0d5f7105c0054 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var totalSum = 0; // this is variable with global scope | |
| function sumTwoNumbers(x, y) { | |
| if(!y) { // this checks if y is undefined | |
| // if y is undefined, it will not | |
| // enter if block | |
| totalSum = totalSum + x; | |
| }else { | |
| totalSum = x + y; | |
| // here total sum becomes 300 | |
| // and totalSum is global by default | |
| } | |
| } | |
| // 100 + 200 + 100 + 300 + 500 + 700 = 1900 | |
| sumTwoNumbers(100, 200); | |
| sumTwoNumbers(100); | |
| sumTwoNumbers(300); | |
| sumTwoNumbers(500); | |
| sumTwoNumbers(700); | |
| console.log(totalSum); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment