Skip to content

Instantly share code, notes, and snippets.

@suhailgupta03
Created May 15, 2023 16:30
Show Gist options
  • Select an option

  • Save suhailgupta03/0cd5ce0cb1c8271c61e0d5f7105c0054 to your computer and use it in GitHub Desktop.

Select an option

Save suhailgupta03/0cd5ce0cb1c8271c61e0d5f7105c0054 to your computer and use it in GitHub Desktop.
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