Chapter 3
Predict
After hoisting, the function looks like this:
function bar() { // Hoisting: function foo() {} // Function declaration hoisted first var foo; // Variable declaration hoisted after the function (no initialization)
// Actual execution starts here:
return foo; // foo
is still the hoisted function
foo = 10; // This line is never reached due to return
foo = “11”; // Dead code (never executed)
}
Why is foo
a Function at return foo
?
-
Function Declaration Hoisting:
function foo()
is hoisted above thereturn
statement and initializesfoo
as a function.
-
var foo
Declaration:- The
var foo
declaration is hoisted after the function, but it doesn’t overwrite the function. At this point,foo
still refers to the function.
- The
-
return foo
Executes Before Any Assignments:- The
return foo
statement is executed before any reassignment likefoo = 10
orfoo = "11"
. - Thus,
foo
still refers to the hoisted function.
- The
-
Dead Code:
- Lines after the
return foo
are not executed becausereturn
ends the function execution.
- Lines after the
Predict 2
Outputs
4 4 4 4
why ?
This is a classic JavaScript closure and scope issue. Let me explain why you’re seeing four “4”s instead of 0, 1, 2, 3:
The issue occurs because var
has function scope, and the value of i
is shared across all the timeouts. By the time the callbacks actually execute, the loop has already completed and i
has reached 4. All four callbacks then reference the same i
, which is now 4.
Here are three ways to fix this:
- Using
let
instead ofvar
(recommended modern solution):
- Using an IIFE (Immediately Invoked Function Expression) to create a new scope:
- Passing the value as a parameter to setTimeout:
The key difference is that let
creates a new binding for each loop iteration, while var
creates a single binding for the entire function scope. When using let
, each callback function gets its own copy of i
, preserved from the iteration in which it was created.
DOM related Javascript questions
- Describe event bubbling.
- Describe event capturing.
- What is the difference between an “attribute” and a “property”?
- Explain the same-origin policy with regards to JavaScript.
- What is the difference between event.target and event.currentTarget?
- What is the difference between event.preventDefault() and event.stopPropagation()?
- What is the DOM?
- How do you select elements with Vanilla JavaScript?
- Explain event delegation in JavaScript.
- what is the purpose of the addEventListener method?
- How do you create and remove elements in the DOM?
- Explain the concept of event propagation.
- How can you prevent the default behaviour of an event?
- What is the purpose of the data- attribute in HTML?
- Describe the difference between innerHTML and textContent.
- How do you handle asynchronous code in JavaScript?
Micelleneous
- What language constructions do you use for iterating over object properties and array items?
- What are the pros and cons of extending built-in JavaScript objects?
- What is the difference between == and ===?
- Why is it called a Ternary operator, what does the word “Ternary” indicate?
- What is strict mode? What are some of the advantages/disadvantages of using it?
- What are some of the advantages/disadvantages of writing JavaScript code in a language that compiles to JavaScript?
- What tools and techniques do you use debugging JavaScript code?
- Explain the difference between mutable and immutable objects.
- What is an example of an immutable object in JavaScript?
- What are the pros and cons of immutability?
- How can you achieve immutability in your own code?
- Explain the difference between synchronous and asynchronous functions.
- What is event loop?
- What is the difference between call stack and task queue?
- What are the differences between variables created using let, var or const?
- Can you change a property of an object defined via const? How you can change this behavior?
- What are the differences between ES6 class and ES5 function constructors?
- Can you offer a use case for the new arrow => function syntax? How does this new syntax differ from other functions?
- What advantage is there for using the arrow syntax for a method in a constructor?
- What is the definition of a higher-order function?
- Can you give an example for destructuring an object or an array?
- Can you give an example of generating a string with ES6 Template Literals?
- Can you give an example of a curry function and why this syntax offers an advantage?
- What are the benefits of using spread syntax and how is it different from rest syntax?
- How can you share code between files?
- Why you might want to create static class members?
- What is the difference between while and do-while loops in JavaScript?
- What is a promise? Where and how would you use promise?
- Discuss how you might use Object Oriented Programming principles when coding with JavaScript.
TOP - 30 Indian Trivia Questions
1-10: Basics of JavaScript
- What is JavaScript?
- Explain the difference between let, const, and var.
- How does hoisting work in JavaScript?
- Describe the concept of closures.
- Explain the event loop in JavaScript.
- What is the difference between == and ===?
- How do you check the type of a variable in JavaScript?
- What is the use of the this keyword in JavaScript?
- Explain the difference between function declaration and function expression.
- How does the setTimeout function work?
11-20: Functions and Scope
- What is a callback function?
- Explain the concept of a pure function.
- Describe the differences between function.call, function.apply, and function.bind.
- What is the purpose of the arguments object in a funct
- How do you create a closure in JavaScript?
- What is the use of the bind method?
- What is the difference between a shallow copy and a deep сору?
- How does the call stack work in JavaScript?
- Explain the concept of function currying.
- How can you avoid callback hell in JavaScript?
21-30: Objects and Prototypes
- What is prototypal inheritance?
- How do you create an object in JavaScript?
- What is the purpose of the prototype property in Javas
- Explain the difference between Object.create and the constructor pattern.25. How do you add a property to an object in JavaScript?
- What is the hasOwnProperty method used for?
- How can you prevent modification of object properties in JavaScript?
- Describe the use of the new keyword.
- Explain the concept of Object Destructuring in JavaScript.
- What is the difference between null and undefined?