Interview Questions


1) What is displayed in the Console ?
This expression is shorthand for "b = 3; var a = b;"

( function() { 
   var a = b = 3;
}) ();

// when strict mode is turned off
console.log( typeof(a) ); // "undefined" - because "a" defaults to local scope
console.log( typeof(b) ); // "number" - because "b" defaults to global scope

// when strict mode is turned on
console.log( typeof(a) ); // Run-time Error: variable undefined in strict mode

2) What is displayed in the Console ?

console.log( mul(2)(3)(4) ); 
function mul (x) {
   return function (y) { // anonymous function
      return function (z) { // anonymous function
         return x + y + z;
      };
   };
};

3) What is displayed in the Console ?

var myObject = { 
   foo: "one";
   func: function(){
      var self = this;
      console.log( this.foo );
      console.log( self.foo );
      ( function() {
         console.log( this.foo ); // this is undefined
         console.log( self.foo );
      }() );
   }
};
myObject.func();

4) Can you give some examples of built-in Objects ?

global 
Function
Object
Array
Math
Date
RegExp
JSON
Error

5) How would you create a module using an Immediately Invoked Function Expression ?
This is called "Revealing Module Pattern".
This encapsulates private variables and methods in a closure scope.

var Module = ( function() { 
   var _fullname = "Russell Proctor";
   function setMyPrivateFullName(newValue) {
      _fullName = newValue;
   }
   function getMyPrivateFullName() {
      return _fullname;
   }

   return {
      setFullName : setMyPrivateFullName,
      getFullName : getMyPrivateFullName
   };
}) ();
console.log( Module.getFullName() );
Module.setFullName("Another Name");
console.log( Module.getFullName() );

6) How would you create a module using the CommonJS approach ?
(require and module.exports)
This is the approach used by Node.js

const { a, b} = require('module-name');  
exports.method = str => str.toUpperCase();

7) How would you create a module using the Asynchronous Module Definition (AMD) ?
define and require


8) How would you create a module using ES 2015 ?
The variables and functions in a module are not available anywhere else unless the module explicitly exports them.


9) What is a Promise Object ?
The promise object (or deferred object) is a way of scheduling work to be done on a value that might not yet be obtained.
This lets you write non-blocking logic that executes asynchronously without having to write any plumbing code.
The promise object allows you to chain operations together.
The promise object is implemented in WinJS and JQuery.
The promise object can be in one of three states:
pending -
resolved -
rejected -


10) Write code that executes if the promise object fails ?
You can use the fail method to execute code when the asynchronous call fails.

function abcAsync() { 
    var promise = timeoutAsync(2000);
    promise.fail(function () { alert('failed!') });
    return promise;
};

11) Write code that executes regardless of success or failure ?

function abcAsync() { 
    var promise = timeoutAsync(2000);
    promise.always(function () { alert('this always runs!') });
    return promise;
};

12) Write code that executes chaining using nesting ?
When you want to chain several asynchronous calls together you can nest them

function abcAsync() { 
    var firstPromise = timeoutAsync(2000);
    firstPromise.done(function () {
        var secondPromise = timeoutAsync(3000);
        secondPromise.done(function () {
            var thirdPromise = timeoutAsync(4000);
            thirdPromise.done( function () { alert('done!') } );
            });
        });
    });
    return firstPromise;
};

13) Write code that executes chaining using the pipe method ?

function abcAsync() { 
    var firstPromise = timeoutAsync(2000);
    var secondPromise = firstPromise.pipe(function () {
        return timeoutAsync(3000);
    });
    var thirdPromise = secondPromise.pipe(function () {
        return timeoutAsync(4000);
    });
    thirdPromise.done(function () { alert('done!') });
    return thirdPromise;
};

14) Write code to make an asynchronous AJAX call using XMLHttpRequest ?

function fetchAjaxAsync(url, callback, errorCallback) { 
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      if (xhr.status == 200) {
        processResult();
      }
      else {
        errorCallback(new Error(xhr.statusText));
      }
   }
  }
  xhr.open(url, "GET", true);
  xhr.send();
}

15) Write code to make an asynchronous AJAX call using the Fetch API ?
This API provides a global fetch method (window.fetch)
The Fetch API uses promises so you can chain multiple calls together instead of nesting them.

fetch(url). 
   then( function (response) {
      return response.json()
   })
   .then( function (myJSON) {
      console.log( JSON.stringify(myJSON) )
   })

16) What is Namespacing and how is it used ?
?


17) When should you use the setTimeout, setInterval and clearInterval functions ?
?


18) What are the disadvantages to using innerHTML ?
) content is replaced everywhere
) unable to append to the existing content
) does not provide validation so it is possible to insert invalid HTML


19) Can you explain the 'window.onload' and 'onDocumentReady' events ?
window.onload - this is only run once all the information on the page has been loaded.
onDocumentReady - loads the code after the DOM has loaded.


20) What are some of the new features available in ES 2015 ?
Support for constants
block scope support for constants and functions
classes
promises
arrow functions
enhanced object properties
extended literals
template literals
modules (import /export)
destructuring assignment


21) What is the 'Spread' operator in ES 2015 ?
The spread operator '...' can take either an array or an object and expand it into its set of Items.
For arrays this operator lets you drop an array inside another array and get its values.
In the array context, this operator is also called the 'spread element'.

const array1 = [1,2]; 
const array2 = [...array1, 3, 4];
// array2 = [1, 2, 3, 4]

For objects this operator is the equivalent of Object.assign, copying the values of one object into a new object.

const object1 = { a:1, b:2}; 
const object2 = { c:3, ...Object1 };
// object2 = { a:1, b:2, c:3 }

22) What is Transpiling ?
This term means taking source code written in one language and transforming it into another language that has the same (or similar) level of abstraction.
The best example is compiling TypeScript to JavaScript.


23) What is JSON ?
JavaScript Object Notation is a lightweight data-interchange format and is easy for code to parse and generate.
JSON is built on two structures. A collection of name/value pairs and an ordered list of values.


24) What is YAML ?
YAML is a superset of JSON and is a human friendly data serialisation standard.
This is similar to XML.


25) What is AJAX ?
AJAX is the acronym for Asynchronous JavaScript and Xml
This refers to a set of techniques used on the client side to create asynchronous web applications.



© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrev