Javascript Scope in variables and functions

I was given this snippet to debug in one of my interviews.

var module = (function sumModule(){
  
  var a = 0;
  var b = 0;
  
  const init = (a,b) =>{
    a = a;
    b = b;
  }
  
  function sum() {
    return a+b;
  }
  
  return {
  sum: sum,
  init
  }
  
})();

module.init(1,2);
console.log(module.sum())

The value being returned is 0 (0+0), the assignment of a and b in func init didn’t overwrite the global var a and var b. Why is this so, can someone please explain?

NOTE: I was told to fix it without renaming the function parameters (a,b).

Answer

Instead of renaming the parameters you can also refer to the global variables via their namespace like you do with the init function.

*Or just rename the global variables, if that is allowed

var module = (function sumModule(){
  var a = 0;
  var b = 0;
  
  const init = (a, b) => {
    module.a = a;
    module.b = b;
  }
  
  function sum() {
    return module.a + module.b;
  }
  
  return {
    sum: sum,
    init,
  }
  
})();

module.init(1, 2);
console.log(module.sum())