Saturday 18 August 2018

Top Java Script Interview Questions and Answers with Examples 2018



1) What is Pure Function?

A pure function is a function

  • which accepts the same input and returns the same output.
  • It will not produce any side effects.
Example 1:


Example 2:

    Math.max( ) is predefined pure function in java script.
    It will accepts number of arguments and return the largest argument value.


2) What is Function Composition?

         Function composition is the process of combining two or more functions to produce a new function.

Example:
In the above example split( ), map( ) and join( ) functions are used to produce a new function.
So, It is called as a Function Composition(Composable Function)

3) What is a Promise?

A promise is an object which can be returned synchronously from an asynchronous function. It has 3 states:
  • Resolved: resolve() will be called.
  • Rejected: reject() will be called.
  • Pending: not yet resolved or rejected.
Example:

4) What is functional programming?

         Functional programming is the process of developing a software by using pure functions
Functional programming is a declarative programming and application state flows through pure functions.
The main features of functional programming are,
  • It is a programming paradigm.
  • Style of coding like way of approaching a code.
  • It is safer and easy to debug the code.
  • It avoids the side effects with pure functions.
  • It have Higher order functions.
  • It avoids the mutability.
Example 1:

Example 2:

5) What are first-class functions?

      First class functions are just like other variables in JavaScript functions. It is an instance of the Object type and have properties and a link back to its constructor method. We can pass it as a parameter to another function.

Example:

6) What is Currying?

       Currying is the process of transforming a function with multiple arity into the same function with the less arity.

Example:

7) What is Functor?

         A functor is nothing but something that can be mapped over.

Example:
The resultant array we can call it as a functor.

8) What is hoisting?

         Hoisting is nothing but a variable can be used before variable declaration.

Example:
In the same way function hoisting is, calling a function before its declaration.

Example:

9) What is function expression?

          Function expression is also called as first-class function.

Example:

10) What is immediate invoking function?

          Anonymous functions are also called as immediate invoking function.

Example:

11) What will be the output of the following code snippet?


The output is 1. For explanation refer question 6.

12) What will be the output of the following code snippet?


The output is 1 and 2.

13) What is prototype?

          Prototype is an object. All java script objects inherit the properties and methods from a prototype.

Example:

14) What is the difference between map( ), filter( ) and reduce( )?

            map( ) function is used to transform the array values into an array of items.

Example:

filter( ) is used to select the items based on certain condition.

Example:

reducer( ) is used to reduce an array into a value.

Example:

15) What will be the output of the following code snippet?

                       console.log( true + true );

Solution:
 
        In JavaScript, when you are trying to make arithmetic operations(like addition, subtraction, multiplication). true is converted to 1 and false is converted to 0.

        So, the solution for above  code snippet is 2.