Angular Interview Questions
Angular Interview Questions
AngularJS
Note: There are Promise libraries out there that support cancellation, but ES6
Promise doesn't so far.
An Observable is like a Stream (in many languages) and allows to pass zero or more
events where the callback is called for each event. Often Observable is preferred over
Promise because it provides the features of Promise and more. With Observable it
doesn't matter if you want to handle 0, 1, or multiple events. You can utilize the same
API in each case. Observable also has the advantage over Promise to be cancelable.
If the result of an HTTP request to a server or some other expensive async operation
isn't needed anymore, the Subscription of an Observable allows to cancel the
subscription, while a Promise will eventually call the success or failed callback even
when you don't need the notification or the result it provides anymore. Observable
provides operators like map, forEach, reduce, ... similar to an array. There are also
powerful operators like retry(), or replay(), ... that are often quite handy.
Promises vs Observables
o Promises:
i. returns a single value
ii. not cancellable
o Observables:
1. works with multiple values over time
2. cancellable
3. supports map, filter, reduce and similar operators
4. proposed feature for ES 2016
5. use Reactive Extensions (RxJS)
6. an array whose items arrive asynchronously over time
https://stackoverflow.com/questions/36064303/what-are-the-differences-between-
observables-and-promises-in-javascript
Some of the events are applicable for both component/directives while few are
specific to components.
Component-specific hooks:
Most of the enterprise application contains various modules for specific business
cases. Bundling whole application code and loading will be huge performance impact
at initial call. Lazy lading enables us to load only the module user is interacting and
keep the rest to be loaded at runtime on demand.
Lazy loading speeds up the application initial load time by splitting the code into
multiple bundles and loading them on demand.
Every Angular application must have one main module say AppModule. The code
should be splitted into various child modules (NgModule) based on the application
business case.
JavaScript Interview Questions
What is Closure?
A closure is a feature in JavaScript where an inner function has access to the
outer (enclosing) function’s variables. Closures is a neat way to deal with
scope issues.
The closure has three scope chains:
it has access to its own scope — variables defined between its curly brackets
it has access to the outer function’s variables
it has access to the global variables
Suppose, you want to count the number of times user clicked a button on a
webpage. For this, you are triggering a function on onclick event of button to update
the count of the variable.
<button onclick="updateClickCount()">click me</button>
Now there could be many approaches like:
1) You could use a global variable, and a function to increase the counter:
var counter = 0;
function updateClickCount() {
++counter;
// do something with counter
}
But, the pitfall is that any script on the page can change the counter, without
calling updateClickCount().
2) Now, You might be thinking of declaring the variable inside the function:
function updateClickCount() {
var counter = 0;
++counter;
// do something with counter
}
But, Hey! Every time updateClickCount() function is called, the counter is set to 1
again.
function countWrapper() {
var counter = 0;
function updateClickCount() {
++counter;
// do something with counter
}
updateClickCount();
return counter;
}
This could have solved the counter dilemma, if you could reach
the updateClickCount() function from the outside and you also need to find a way to
execute counter = 0 only once not everytime.
return function(){
++counter;
// do something with counter
}
})();
The self-invoking function only runs once. It sets the counter to zero (0), and returns
a function expression.
This way updateClickCount becomes a function. The "wonderful" part is that it can
access the counter in the parent scope.
The counter is protected by the scope of the anonymous function, and can only be
changed using the add function!
return function(){
++counter;
document.getElementById("spnCount").innerHTML=counter;
}
})();
</script>
<html>
<button onclick="updateClickCount()">click me</button>
<div> you've clicked
<span id="spnCount"> 0 </span> times!
</div>
</html>
React JS interview
A virtual DOM is a lightweight JavaScript object which originally is just the copy of the
real DOM. It is a node tree that lists the elements, their attributes and content as
Objects and their properties. React’s render function creates a node tree out of the
React components. It then updates this tree in response to the mutations in the data
model which is caused by various actions done by the user or by the system.
This Virtual DOM works in three simple steps.
1. Whenever any underlying data changes, the entire UI is re-rendered in Virtual
DOM representation.
2. Then the difference between the previous DOM representation and the new
one is calculated.
3. Once the calculations are done, the real DOM will be updated with only the
React vs Angular
i. componentWillMount() – Executed just before rendering takes place both on the client
as well as server-side.
ii. componentDidMount() – Executed on the client side only after the first render.
iii. componentWillReceiveProps() – Invoked as soon as the props are received from the
parent class and before another render is called.
iv. shouldComponentUpdate() – Returns true or false value based on certain conditions. If
you want your component to update, return true else return false. By default, it returns
false.
v. componentWillUpdate() – Called just before rendering takes place in the DOM.
vi. componentDidUpdate() – Called immediately after rendering takes place.
vii. componentWillUnmount() – Called after the component is unmounted from the DOM. It
is used to clear up the memory spaces.
1. They do not maintain their own state 1. They maintain their own state