- How can you create a function in Javascript?
Using the function
keyword at the start of a statement allows a function to be called anywhere. It has a set of parameters and a body that contains the statements to be executed when the function is called.
function(param1, param2) {
// some useful output...
}
Another method calls the function just after it is created:
const square = function(x) {
return x * x;
};
console.log(square(12));
// → 144
And more simply with declaration notation, the function name is between the keyword function
and the parameters in {}.
function add(x, y) {
return x + y;
};
- What is the difference between var and let in regards to scope?
var
(variable) was pre-2015 JS binding declaration… This keyword is visible
throughout the whole function that they appear in—or throughout the global
scope, if they are not in a function.
let
(and const
) are local to the block that they are declared in. (If in a loop, code before/after the loop will not see
it)
- What is a pure function?
A function that produces a value that is not affected by other code. I.e. it will return the same values given the same parameters.