Ask Your Question
2

Can the value assigned to the movies variable in the injectdb() function be saved for use in the getMovies() function?

asked 2022-05-17 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-07-31 11:00:00 +0000

scrum gravatar image

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']
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-05-17 11:00:00 +0000

Seen: 9 times

Last updated: Jul 31 '22