Sitemap
Nagarjun deepak

Nagarjun deepak

Web Development | Cinema | Comics

JavaScript Q/A

3 min readDec 2, 2019

This article consists of some JavaScript questions and answers asked in web developer interviews. special thanks to Adi S for the questions.

Press enter or click to view image in full size
Photo by Oskar Yildiz on Unsplash

1: What is the difference between apply and call in JavaScript?

The difference is that apply lets you invoke the function with arguments as an array; call requires the parameters be listed explicitly. A useful mnemonic is "A for array and C for comma."

theFunction.apply(valueForThis, arrayOfArgs)

theFunction.call(valueForThis, arg1, arg2, ...)

2: What is the difference between an object and an array?

An Array has all the features of an object with additional features (you can think of an array like a sub-class of an object) where additional methods and capabilities are added in the Array sub-class. In fact, typeof [] === "object" to further show you that an array is an object.

The additional features consist of a magic .length property that keeps track of the number of items in the array and a whole slew of methods for operating on the array such as .push(), .pop(), .slice(), .splice(), etc...

An Object gives you the ability to associate a property name with a value as in:

var x = {};
x.foo = 3;
x["whatever"] = 10;
console.log(x.foo); // shows 3
console.log(x.whatever); // shows 10

Object properties can be accessed either via the x.foo syntax or via the array-like syntax x["foo"]. The advantage of the latter syntax is that you can use a variable as the property name like x[myvar] and using the latter syntax, you can use property names that contain characters that Javascript won't allow in the x.foo syntax.

A property name can be any string value.

3: What type of data structure is a stack?

Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

Get Nagarjun deepak’s stories in your inbox

Join Medium for free to get updates from this writer.

Check out the JS Bin example for more details.

4: What type of data structure is a queue?

A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).

Check out the JS Bin example for more details.

5: What is event delegation in JavaScript?

DOM event delegation is a mechanism of responding to ui-events via a single common parent rather than each child, through the magic of event “bubbling” (aka event propagation).

Event bubbling provides the foundation for event delegation in browsers. Now you can bind an event handler to a single parent element, and that handler will get executed whenever the event occurs on any of its child nodes (and any of their children in turn). This is event delegation. Here’s an example of it in practice:

<ul onclick="alert(event.type + '!')">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>

6: what is Event Bubbling and Event Capturing?

Event Bubbling is the event starts from the deepest element or target element to its parents, then all its ancestors which are on the way to bottom to top. At present, all the modern browsers have event bubbling as the default way of event flow.

Event Capturing is the event starts from top element to target element. Modern browser doesn’t support event capturing by default but we can achieve that by code in JavaScript.

7: What is hoisting?

Hoisting is when the JavaScript interpreter moves all variable and function declarations to the top of the current scope. It’s important to keep in mind that only the actual declarations are hoisted, and that assignments are left where they are.

Hoisting is done during the interpreter’s first run through the code. Also JavaScript only hoists declarations, not the initialisations.

will update more…

--

--

Nagarjun deepak
Nagarjun deepak

Written by Nagarjun deepak

Web Development | Cinema | Comics