How can you create a function in Javascript?
There are three different ways. One way is to define a const like so…
const cube = function(x) {
return x * x * x;
};
Then use the function keyword after = with any number of parameters. Then use return to get the result.
Alternatively you can use the declarative form (which is closer to what I’m used to)
function cube(x) {
return x * x * x;
}
Alternatively one can also write the same thing like this…
const cube = x => x * x * x;
Then you can call like this
cube(4);
// 64
What is the difference between var and let in regards to scope?
Let is only visible from within the block it’s declared in or in nested blocks below it. Var is like declaring a global variable which has scope throughout the unit it is written in.
What is a pure function?
A function that has no side effects and returns a value.