The function declaration
function pickColor(){...}
is moved to the top of the scope (hoisted first) along with any variables declared with var
.
where as function expression declared with let
is only accessed when the interpreter comes across that line of code.
Example -
let two = () => {
console.log('something else');
}
var x = 'demo'
let y = 'demo123'
function one() {
console.log('something');
};
function three(){
one();
}
variable x
, function one(){...}
, function three(){...}
are moved to the top of the scope as soon as the execution starts,
x
is undefined
until the line
x = 'demo'
then
x
is assigned a value of 'demo'
.
Variable y
is not initiated or assigned any value until the line
let y = 'demo123'
since let
is used to initiate the variable, same goes for the function expression initiated with let
.