Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, the value assigned to the movies variable in the injectdb() function can be saved for use in the getMovies() function if it is declared in a scope that is accessible to both functions. One way to achieve this is by declaring the variable outside of both functions, in the global scope. Then, the value can be set in the injectdb() function and retrieved in the getMovies() function.

For example:

let movies = []; // declare the variable in the global scope

function injectdb() {
  // do something to populate the movies array
  movies = ['The Shawshank Redemption', 'The Godfather', 'The Dark Knight'];
}

function getMovies() {
  return movies;
}

injectdb();
console.log(getMovies()); // output: ['The Shawshank Redemption', 'The Godfather', 'The Dark Knight']