Professional Documents
Culture Documents
When this script runs it will print undefined. This is because even though the variable is declared after
the console.log, it will originally be declared before that. But the assignment will be done after
the console.log. So it will simply print undefined.
Running the above code results in displaying an alert box with the string ‘Javascript’. When running the
outer function makeFunc, it completes its execution and returns the inner function. Then later, when we
run the inner function displayName, it displays the alert box, even though the outer function has already
finished its execution. The reason is the inner function still remembers the outer functions scope (i.e.,
variable name) and when executing later it displays the correct name. This is also an example for how
closures work in JavaScript.
Closures have important role in callbacks and asynchronous programming. The famous nodejs, server side
JavaScript environment relies heavily on closures and events.
9. What does async and defer attributes do? Why they are used in the script tag?
By default, when the script tag is executed in the browser, it will stop parsing the HTML and getting the
script. The parsing will not be resumed until the script is received and validated.
When using async in <script> tag as <script async>, the browser will download the file and do the HTML
parsing at the same time. Once the download completed, it will pause the HTML execution and run the
script.
Now if we use defer as <script defer>, the browser downloads the script during the HTML parsing, but
only execute the script after the parser has completed.
IE9 and below have some bugs in their implementation of defer such that the execution order isn’t
guaranteed.
10. What is shift() and push? How they are differing from each other?
The shift() removes the first element from an array and return it. On the otherhand, unshift() adds the
given element at the start of an array.
The push() adds the given element at the end of the array. And, pop() removes the last element from an
array.
If we explicitly return something inside the function, then the this binding will be ignored and an empty
object will be assigned to the caller variable.
When invoking the printNumber(), the system prints 5. Because, here this resolves to the global
object window. So window.num yields 5 here.
Implicit binding: In this case, when the function has a context-object, this resolves to the owning object
or containing object. The below example illustrates this.
When we call the object obj's printNumber method, obj.printNumber(), we will get 6. Because the obj is
the this for the printNumber().
Explicit binding: By this way we are forcing a function to use an object to resolve the this. This can be
achieved by using call and apply.
In the above code, we are invoking the printNumber function with call and explicitly binding
the obj object to it. So the result will be 6.
The methods call and apply are identical, but they behave differently with their parameters. apply expects
the parameters to be in array format, where callexpects separately.
new binding: The new binding is invoked when the function is invoked with new keyword. We are
using new keyword in JavaScript to construct an object out of a function.
When we call a function with new keyword, a new object is constructed and set this newly constructed
object to this in the function.
Final Note: this is referring the object to be bound for an executing function, but determining the object is
based on from where and how the function was called. Other than this, ES6 arrow functions have lexical
scope, in which they refer the enclosing function. Additionally, in event handlers the this is set to the
element which fired the event.
14. What are all the best practices for writing JavaScript Code? How do you write better code?
This has many different answers. Some of the best ways are:
Always initialize the variables before you use them.
Always use === equals instead of ==.
Wrapping code that could thrown errors at run time and feature-based code in try...catch block.
Don’t pollute global scope and don’t use global variables.
Always use "use strict";.
Use lint tools to validate JavaScript code and avoid any potential risks.
Wrap your code in an anonymous function or Immediately Invoked Function Expressions (IIFE), so that it
will affect the global scope.
Give clear name for variables and functions.
Give comments wherever needed so that the other developer understands the code better.
Place all your scripts at end.
Reduce DOM manipulations and if you need to add large dom elements by script,
use document.createDocumentFragment.
When we call bar, the first frame is created in the Call Stack with the functions arguments and local
variables. The bar function calls foo from itself, which will create another frame and pushed to the top of
the Call Stack. This stack also contains the foo's arguments and local variables. When foo returns a value
or complete execution, it is removed from the Stack and when bar returns the Stack is empty.