Skip to content

Instantly share code, notes, and snippets.

@tarun-nagpal-github
Last active March 26, 2020 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarun-nagpal-github/c383e7694abe14daef4c018e8bf52eb9 to your computer and use it in GitHub Desktop.
Save tarun-nagpal-github/c383e7694abe14daef4c018e8bf52eb9 to your computer and use it in GitHub Desktop.
JavaScript - Interview Puzzles
// What is the output and Why
const myData = [1,2,3,4];
console.log(myData[4]);
// Answer - undefined
// What is the code below doing and how we can optimize it
const items = document.querySelctorAll('#container > div');
items.forEach(el => {
el.addEventListener('click', () => {
console.log('Event Clicked');
});
});
// Answer - https://javascript.info/event-delegation
// Question - What will happen when we register 2 click events on 1 button
// Why the two this has diffrent values
var hello = document.getElementById("myId");
hello.addEventListener("click", () => {
console.log(this);
});
hello.addEventListener("click", myFunction);
function myFunction(){
console.log("HELLO WORLD 3");
console.log(this);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment