Q:

JS variable is undefined (not remembered) inside passed function

i have a function that takes another function and its variables and executes that function, my problem is that the variables i'm passing is becoming undefined,

function A(type = 'default_type', B = function(){}) {
  B();
}

function Parent() {
  var data = "hello"
  
  A('my_type', data => {
    console.log(data);
  });
}
Parent();

it works when i do this: 

function A(type = 'default_type', data,B = function(){}) {
  B(data);
}

function Parent() {
  var data = "hello"
  
  A('my_type', data => {
    console.log(data);
  });
}
Parent();

but i don't want to do this, because i might need to pass multiple variables in the futur

A:

You use data twice, change the function declaration inside your call to A() to a different name, so you can access your "Hello" data.

function A(type = 'default_type', B = function(){}) {
  B();
}

function Parent() {
  var data = "hello"
  
  A('my_type', d => {
    console.log(data);
  });
}
Parent();

