Q:

Can I define a simple function within a function in order to encapsulate and reuse for different cases in the function?

Let’s say that I have a logOut() function and a user may be logged in via one of several different ways.  So in the logOut() function I might check for Facebook auth user context.  If that exists then I would execute the code to log the user out of Facebook and then navigate to the Login Home page.  Similarly a user may be logged in via Apple and I would handle this scenario in a similar way. Would it be possible to define some type of function variable at the beginning of this logOut() function like:
const navigateHome = props.navigation.navigate('LoginHome');

And then reuse the navigateHome variable as an instruction based on the case in the code like:
if(loggedInViaFacebook){
   navigateHome;
}else if(loggedInViaApple){
   navigateHome;
}

This is a very simple example but I just want to understand if this is conceptually possible so I can leverage it as a technique.

A:

Functions may contain other functions. They can be called within the same scope (assuming you're using const instead of var).
function someParentFunction() {

  const navigateHome = function() {
    props.navigation.navigate('LoginHome');
  }

  if(loggedInViaFacebook){
    navigateHome();
  }
  else if(loggedInViaApple){
    navigateHome();
  }

}

