How can you create a function in Javascript?
There are different ways to define functions in JavaScript
-
const myFunction = function(argument1, argument2) { return "I recieved " + argument1 + " and " + argument2 + "." }; // this method assigns value of function to a binding
-
function myFunction(argument1, argument2) { return "I recieved " + argument1 + " and " + argument2 + "." } // This is a function being declared and does not need to exist before being called. Requires end semi-colon
-
const myFunction = (argument1, argument2) => { return "I recieved " + argument1 + " and " + argument2 + "." }; // Arrow function which requires to end with the semi-colon
What is the difference between var and let in regards to scope?
When bindings are declared using the var keyword they are global in nature and can be seen from all parts of the program even if called inside a function. let is more specific than var and gives more precision as to the scope of the binding being declared. let can be global if declared in the main body of a program but if bindings are declared with let inside a for loop for example that binding can only be seen inside that loop and not from outside. The same if let declares a binding inside a function. That binding is not visible from outside the function but is visible to functions inside the parent function that declares the binding.
What is a pure function?
A pure function is a function that does not depend on any side effects nor does it depend on any bindings outside of its self. The function should always be able to return the expected value no matter the state of the current running environment.