What does the return statement do? When do I use return statements?
Answer
return statements can be used inside functions to: 1. end function execution, which will stop any code under the return statement from running and 2. return a value when the function is called. return statements cannot be used outside of functions. The default return value is undefined when no return value is specified.
For example:
const myFunc = () => {
console.log('hello'); //function that logs hello to the console, no return value is specified
}
console.log(myFunc()); //logging the function call executes the function and logs the return value as well, in this case no return value was specified so undefined also logs to the console
When declaring a constant variable, the only thing constant is the binding between the variable and its value. Many people think that constant variables cannot be mutated. This is not true. If we assigned an object to a constant variable, we can mutate it
However, if we assign a primitive to a constant variable, or to a reassignable variable, it cannot be mutated. This is simply because primitives cannot be mutated