Aug 4, 2024
let
, var
, and other concepts.let
and var
let
creates block-scoped variables.let
cannot be accessed outside their block.let a = 1
and let b = 2
are defined in a block and accessed outside, it will throw a ReferenceError
.var
and let
console.log
a variable before it is assigned?var
, it returns undefined
because var
is hoisted.let
, it throws a ReferenceError
due to the Temporal Dead Zone (TDZ).name
variable:
var name = ''
is declared, console.log(name)
outputs an empty string, not undefined
.var x = 20
and a local var x = 10
will cause the local variable to shadow the global one.x
inside the function returns undefined
because the function creates its variable before the assignment.console.dir
can show how variables are bundled with the inner function.start
, end
, and then timeout
.console.log
statements are synchronous and execute in order.setTimeout
is asynchronous and does not execute until the call stack is empty, even if the timeout is set to 0 milliseconds.