User FAQs

If you have a question, please send it to us.

1) Can you describe a Function ?
A function is a block of code designed to perform a specific task.
A function is a callable object
A function that is a member of an object is called a method
A function is executed when it is called (or invoked).
A function always returns a value.

function func1() { 
   return 10;
console.log( func1() ); // 10

2) Can you describe the return statement ?
If a function contains a 'return' statement the function will stop executing.
If no specific value is returned by this statement then 'undefined' is returned.

function func1() { 
function func2() {
console.log( func1() ); // undefined
console.log( func2() ); // undefined

3) How many different ways can you create a function ?
*) Function declaration
*) Function expression
*) Function constructor
*) Arrow function

4) What is the difference between a Function Declaration and a Function Expression ?
Function declarations are hoisted to the top but function expressions are not.
You should always include a semicolon after a function declaraction and a function expression.

// function declaration always starts with the function keyword 
function myfunc1(one, two) {
   return one + two;

// function expression with assignment ?
let myfunc2 = function(one, two) {
   return one + two;
console.log ( myFunc1(10,5) ); // 15
console.log ( myFunc2(10,5) ); // 15

5) Can you describe the Function Constructor ?
Functions can be defined using the 'function' keyword
Functions can also be defined using a built-in constructor called 'Function'.

var myFunc3 = new Function("one", "two", "return one + two"); 
console.log ( myFunc3(10,5) ); // 15

6) What is a Self-Executing Function ?
This is an anonymous function that is executed immediately and does not pollute the global namespace.
Also called an Immediately Invocable Function Expression ((IIFE).

(function() { 
}) ();

This could be written using the arrow function.

( () => { 
}) ();

The first pair of parentheses converts the function declaration into a function expression.
The second pair of parentheses executes the function.
There is alternative syntax that can be used.

(function() { 
}() ); // douglas crockford

Another alternative is to prefix the function expression with any unary operator

7) Can you explain the difference between 'Assigning' an anonymous function and 'Executing' an anonymous function.

// assigning 
let myfun1 = function() {
// executing
let myfun1 = ( function() {
}() );

8) Can you have Optional Arguments ?
Yes. All arguments are optional by default. If you do not pass in an argument, its parameter will be undefined.
Also if you pass in too many arguments, they will just be ignored.
This has benefits as well as drawbacks.

9) Can you describe the arguments object ?
This is an array-like object that is accessible from inside functions that contains the values of the arguments passed to that function.

function func1 (a, b) { 
   console.log( arguments[0] ); // output: 1
   console.log( arguments[1] ); // output: 2

10) Can you describe a Nested Function ?
It is possible to nest function declarations inside function declarations.
Nested functions are private to the function in which they are defined

function multiplySomeNumbers(arg1, arg2) { 
   return doubleThis(arg1) / arg2;
   function doubleThis(arg3) {
      var result = arg3 * 2;
      return result;

11) What is a Promise ?
This is the agreement of a future value.
Its a transaction

12) How does it solve the inversion of control issue.
Instead of passing the continuation in it passes something back Which allows me to decide what the next step is

13) What are the problems with Callbacks ?
Handing the control of the rest of your program to a third party or utility.
How do you know the utility is going to call your callback functions correctly.
Callbacks suffer from the "Inversion of Control".

14) How do you pause a Generator ?
Calling the yield keyword

15) How do you resume a Generator ?
Using the next method

16) How can be combine Generators and Promises ?
The generator yields out a promise
When the promise finishes this restarts the generator

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