Functions are a type of binding created by using the keyword “Function” to begin an expression, where the function can have a set of parameters defined, as well as a body which contains all the statements to be executed by the code when the function is used. The body must be contained within braces.
All bindings defined with “let” are local to the block they are defined in, whereas “var” bindings are visible throughout the whole functions that they appear in, and if they don’t appear in a function then they are recognized on the global scope.
A pure function is defined as a value-producing function type that returns no side effects and also does not depend on any side effects from other code (it cannot read global bindings whose value might change because of this, for example). Upon calling a pure function with the same arguments it will always produce the same value.
There are four ways a function can be created in JavaScript. They are as follows:
(i) A function as a statement
function Add(num1,num2){
let sum = num1+ num2;
return sum;
}
let res = Add(7,8);
console.log(res); // 15
(ii) A function as an expression
let add = function a(num1,num2){
let sum = num1+ num2;
return sum;
}
let res = add(8,9);
console.log(res);// 17
(iii) A function as an arrow function
var add = (num1, num2)=> num1+num2;
let res = add(5,2);
console.log(res); // 7
(iv) A function created using the Function constructor
var add = Function(‘num1’,‘num2’,‘return num1+num2’);
let res = add (7,8);
console.log(res); // 15
2. What is the difference between var and let in regards to scope?
var and let are both used for variable declaration in javascript but the difference between them is that var is function scoped and let is block scoped.
It can be said that a variable declared with var is defined throughout the program while a variable declared with let is defined just to be used inside a function.
3. What is a pure function?
A function must pass two tests to be considered “pure”: 1. Same inputs always return the same outputs 2. No side-effects
A function is created with an expression that starts with the keyword “function”. It has a set of parameters and a body which contains the statements that will be executed when the function is called.
What is the difference between var and let in regards to scope?
Bindings with “Let” and “Const” are local to the block they are declared in.
Bindings with “var” are visible locally, if they’re in a function, or globally if not in a function.
What is a pure function?
Specific type of value producing function that not only has no side effects, but also doesn’t rely on side effects to from other code.
When its called with the same arguments, it always produces the same value.