Skip to content

Instantly share code, notes, and snippets.

@mllrjb
Last active May 29, 2019 21:03
Show Gist options
  • Save mllrjb/7f62d93f720dd04ffe1acd971e6c32e4 to your computer and use it in GitHub Desktop.
Save mllrjb/7f62d93f720dd04ffe1acd971e6c32e4 to your computer and use it in GitHub Desktop.
JavaScript interview question (banana)

What does the following output?

# what will the following code output?

const arr = [5, 9, 4, 12];
for (var i = 0; i < arr.length; i++) {
  setTimeout(function() {
    console.log('Index: ' + i + ', element: ' + arr[i]);
  }, 3000);
}

One possible answer is Index: 4, element: banana. Why?

arr.splice(4, 0, 'banana')

Or the answer might be popcorn. Why?

const log = console.log
console.log = function() { log('popcorn') }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment