1. How can you create a function in Javascript?
Three different ways to create a function -
Method 1: Defining ‘a’ to hold a function value
var a = function(b){
return b*b;
};
console.log(a(3));Method 2: Declaring ‘a’ to be a function
function a(b){
return b*b;
}
console.log(a(3));Method 3: Using arrow function
var a = b => b*b;
console.log(a(3));
2. What is the difference between var and let in regards to scope?
The scope of a variable defined with let is limited to the block in which it is declared.
While the variable declared with var has global scope except when declared inside a function.
3. What is a pure function?
A function is only pure when given the same input always produces the same output.
It produces no side effects, which means that it can’t alter any external state.Example:
Math.random() - Not a pure function
Math.max(2,5,7,8) - Pure function - output is 8 always with same set of input parameters