The explanation is taken from anthe article I wrote at Medium:
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope by the parser which reads the source code into an intermediate representation before the actual code execution starts by the JavaScript interpreter. So, it actually doesn’t matter where variables or functions are declared, they will be moved to the top of their scope regardless of whether their scope is global or local. This means that
console.log (hi); var hi = "say hi";is actually interpreted to
var hi = undefined; console.log (hi); hi = "say hi";So, as we saw just now,
varvariables are being hoisted to the top of their scope and are being initialized with the value of undefined which means that we can actually assign their value before actually declaring them in the code like so:hi = "say hi" console.log (hi); // say hi var hi;Regarding function declarations, we can invoke them before actually declaring them like so:
sayHi(); // Hi function sayHi() { console.log('Hi'); };Function expressions, on the other hand, are not hoisted, so we’ll get the following error:
sayHi(); //Output: "TypeError: sayHi is not a function var sayHi = function() { console.log('Hi'); };ES6 introduced JavaScript developers the
letandconstkeywords. Whileletandconstare block-scoped and not function scoped asvarit shouldn’t make a difference while discussing their hoisting behavior. We’ll start from the end, JavaScript hoistsletandconst.console.log(hi); // Output: Cannot access 'hi' before initialization let hi = 'Hi';As we can see above,
letdoesn’t allow us to use undeclared variables, hence the interpreter explicitly output a reference error indicating that thehivariable cannot be accessed before initialization. The same error will occur if we change the abovelettoconstconsole.log(hi); // Output: Cannot access 'hi' before initialization const hi = 'Hi';So, bottom line, the JavaScript parser searches for variable declarations and functions and hoists them to the top of their scope before code execution and assign values to them in the memory so in case the interpreter will encounter them while executing the code he will recognize them and will be able to execute the code with their assigned values. Variables declared with
letorconstremain uninitialized at the beginning of execution while that variables declared withvarare being initialized with a value ofundefined.I added this visual illustration to better help understanding of how are the hoisted variables and function are being saved in the memory enter image description here
The explanation is taken from an article I wrote at Medium:
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope by the parser which reads the source code into an intermediate representation before the actual code execution starts by the JavaScript interpreter. So, it actually doesn’t matter where variables or functions are declared, they will be moved to the top of their scope regardless of whether their scope is global or local. This means that
console.log (hi); var hi = "say hi";is actually interpreted to
var hi = undefined; console.log (hi); hi = "say hi";So, as we saw just now,
varvariables are being hoisted to the top of their scope and are being initialized with the value of undefined which means that we can actually assign their value before actually declaring them in the code like so:hi = "say hi" console.log (hi); // say hi var hi;Regarding function declarations, we can invoke them before actually declaring them like so:
sayHi(); // Hi function sayHi() { console.log('Hi'); };Function expressions, on the other hand, are not hoisted, so we’ll get the following error:
sayHi(); //Output: "TypeError: sayHi is not a function var sayHi = function() { console.log('Hi'); };ES6 introduced JavaScript developers the
letandconstkeywords. Whileletandconstare block-scoped and not function scoped asvarit shouldn’t make a difference while discussing their hoisting behavior. We’ll start from the end, JavaScript hoistsletandconst.console.log(hi); // Output: Cannot access 'hi' before initialization let hi = 'Hi';As we can see above,
letdoesn’t allow us to use undeclared variables, hence the interpreter explicitly output a reference error indicating that thehivariable cannot be accessed before initialization. The same error will occur if we change the abovelettoconstconsole.log(hi); // Output: Cannot access 'hi' before initialization const hi = 'Hi';So, bottom line, the JavaScript parser searches for variable declarations and functions and hoists them to the top of their scope before code execution and assign values to them in the memory so in case the interpreter will encounter them while executing the code he will recognize them and will be able to execute the code with their assigned values. Variables declared with
letorconstremain uninitialized at the beginning of execution while that variables declared withvarare being initialized with a value ofundefined.I added this visual illustration to better help understanding of how are the hoisted variables and function are being saved in the memory enter image description here
The explanation is taken from the article I wrote at Medium:
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope by the parser which reads the source code into an intermediate representation before the actual code execution starts by the JavaScript interpreter. So, it actually doesn’t matter where variables or functions are declared, they will be moved to the top of their scope regardless of whether their scope is global or local. This means that
console.log (hi); var hi = "say hi";is actually interpreted to
var hi = undefined; console.log (hi); hi = "say hi";So, as we saw just now,
varvariables are being hoisted to the top of their scope and are being initialized with the value of undefined which means that we can actually assign their value before actually declaring them in the code like so:hi = "say hi" console.log (hi); // say hi var hi;Regarding function declarations, we can invoke them before actually declaring them like so:
sayHi(); // Hi function sayHi() { console.log('Hi'); };Function expressions, on the other hand, are not hoisted, so we’ll get the following error:
sayHi(); //Output: "TypeError: sayHi is not a function var sayHi = function() { console.log('Hi'); };ES6 introduced JavaScript developers the
letandconstkeywords. Whileletandconstare block-scoped and not function scoped asvarit shouldn’t make a difference while discussing their hoisting behavior. We’ll start from the end, JavaScript hoistsletandconst.console.log(hi); // Output: Cannot access 'hi' before initialization let hi = 'Hi';As we can see above,
letdoesn’t allow us to use undeclared variables, hence the interpreter explicitly output a reference error indicating that thehivariable cannot be accessed before initialization. The same error will occur if we change the abovelettoconstconsole.log(hi); // Output: Cannot access 'hi' before initialization const hi = 'Hi';So, bottom line, the JavaScript parser searches for variable declarations and functions and hoists them to the top of their scope before code execution and assign values to them in the memory so in case the interpreter will encounter them while executing the code he will recognize them and will be able to execute the code with their assigned values. Variables declared with
letorconstremain uninitialized at the beginning of execution while that variables declared withvarare being initialized with a value ofundefined.I added this visual illustration to better help understanding of how are the hoisted variables and function are being saved in the memory enter image description here
ThisThe explanation is taken from an article I wrote at Medium:
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope by the parser which reads the source code into an intermediate representation before the actual code execution starts by the JavaScript interpreter. So, it actually doesn’t matter where variables or functions are declared, they will be moved to the top of their scope regardless of whether their scope is global or local. This means that
console.log (hi); var hi = "say hi";is actually interpreted to
var hi = undefined; console.log (hi); hi = "say hi";So, as we saw just now,
varvariables are being hoisted to the top of their scope and are being initialized with the value of undefined which means that we can actually assign their value before actually declaring them in the code like so:hi = "say hi" console.log (hi); // say hi var hi;Regarding function declarations, we can invoke them before actually declaring them like so:
sayHi(); // Hi function sayHi() { console.log('Hi'); };Function expressions, on the other hand, are not hoisted, so we’ll get the following error:
sayHi(); //Output: "TypeError: sayHi is not a function var sayHi = function() { console.log('Hi'); };ES6 introduced JavaScript developers the
letandconstkeywords. Whileletandconstare block-scoped and not function scoped asvarit shouldn’t make a difference while discussing their hoisting behavior. We’ll start from the end, JavaScript hoistsletandconst.console.log(hi); // Output: Cannot access 'hi' before initialization let hi = 'Hi';As we can see above,
letdoesn’t allow us to use undeclared variables, hence the interpreter explicitly output a reference error indicating that thehivariable cannot be accessed before initialization. The same error will occur if we change the abovelettoconstconsole.log(hi); // Output: Cannot access 'hi' before initialization const hi = 'Hi';So, bottom line, the JavaScript parser searches for variable declarations and functions and hoists them to the top of their scope before code execution and assign values to them in the memory so in case the interpreter will encounter them while executing the code he will recognize them and will be able to execute the code with their assigned values. Variables declared with
letorconstremain uninitialized at the beginning of execution while that variables declared withvarare being initialized with a value ofundefined.I added this visual illustration to better help understanding of how are the hoisted variables and function are being saved in the memory enter image description here
This explanation is taken from an article I wrote at Medium:
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope by the parser which reads the source code into an intermediate representation before the actual code execution starts by the JavaScript interpreter. So, it actually doesn’t matter where variables or functions are declared, they will be moved to the top of their scope regardless of whether their scope is global or local. This means that
console.log (hi); var hi = "say hi";is actually interpreted to
var hi = undefined; console.log (hi); hi = "say hi";So, as we saw just now,
varvariables are being hoisted to the top of their scope and are being initialized with the value of undefined which means that we can actually assign their value before actually declaring them in the code like so:hi = "say hi" console.log (hi); // say hi var hi;Regarding function declarations, we can invoke them before actually declaring them like so:
sayHi(); // Hi function sayHi() { console.log('Hi'); };Function expressions, on the other hand, are not hoisted, so we’ll get the following error:
sayHi(); //Output: "TypeError: sayHi is not a function var sayHi = function() { console.log('Hi'); };ES6 introduced JavaScript developers the
letandconstkeywords. Whileletandconstare block-scoped and not function scoped asvarit shouldn’t make a difference while discussing their hoisting behavior. We’ll start from the end, JavaScript hoistsletandconst.console.log(hi); // Output: Cannot access 'hi' before initialization let hi = 'Hi';As we can see above,
letdoesn’t allow us to use undeclared variables, hence the interpreter explicitly output a reference error indicating that thehivariable cannot be accessed before initialization. The same error will occur if we change the abovelettoconstconsole.log(hi); // Output: Cannot access 'hi' before initialization const hi = 'Hi';So, bottom line, the JavaScript parser searches for variable declarations and functions and hoists them to the top of their scope before code execution and assign values to them in the memory so in case the interpreter will encounter them while executing the code he will recognize them and will be able to execute the code with their assigned values. Variables declared with
letorconstremain uninitialized at the beginning of execution while that variables declared withvarare being initialized with a value ofundefined.I added this visual illustration to better help understanding of how are the hoisted variables and function are being saved in the memory enter image description here
The explanation is taken from an article I wrote at Medium:
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope by the parser which reads the source code into an intermediate representation before the actual code execution starts by the JavaScript interpreter. So, it actually doesn’t matter where variables or functions are declared, they will be moved to the top of their scope regardless of whether their scope is global or local. This means that
console.log (hi); var hi = "say hi";is actually interpreted to
var hi = undefined; console.log (hi); hi = "say hi";So, as we saw just now,
varvariables are being hoisted to the top of their scope and are being initialized with the value of undefined which means that we can actually assign their value before actually declaring them in the code like so:hi = "say hi" console.log (hi); // say hi var hi;Regarding function declarations, we can invoke them before actually declaring them like so:
sayHi(); // Hi function sayHi() { console.log('Hi'); };Function expressions, on the other hand, are not hoisted, so we’ll get the following error:
sayHi(); //Output: "TypeError: sayHi is not a function var sayHi = function() { console.log('Hi'); };ES6 introduced JavaScript developers the
letandconstkeywords. Whileletandconstare block-scoped and not function scoped asvarit shouldn’t make a difference while discussing their hoisting behavior. We’ll start from the end, JavaScript hoistsletandconst.console.log(hi); // Output: Cannot access 'hi' before initialization let hi = 'Hi';As we can see above,
letdoesn’t allow us to use undeclared variables, hence the interpreter explicitly output a reference error indicating that thehivariable cannot be accessed before initialization. The same error will occur if we change the abovelettoconstconsole.log(hi); // Output: Cannot access 'hi' before initialization const hi = 'Hi';So, bottom line, the JavaScript parser searches for variable declarations and functions and hoists them to the top of their scope before code execution and assign values to them in the memory so in case the interpreter will encounter them while executing the code he will recognize them and will be able to execute the code with their assigned values. Variables declared with
letorconstremain uninitialized at the beginning of execution while that variables declared withvarare being initialized with a value ofundefined.I added this visual illustration to better help understanding of how are the hoisted variables and function are being saved in the memory enter image description here
This explanation is taken from an article I wrote at Medium:
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope by the parser which reads the source code into an intermediate representation before the actual code execution starts by the JavaScript interpreter. So, it actually doesn’t matter where variables or functions are declared, they will be moved to the top of their scope regardless of whether their scope is global or local. This means that
console.log (hi); var hi = "say hi";is actually interpreted to
var hi = undefined; console.log (hi); hi = "say hi";So, as we saw just now,
varvariables are being hoisted to the top of their scope and are being initialized with the value of undefined which means that we can actually assign their value before actually declaring them in the code like so:hi = "say hi" console.log (hi); // say hi var hi;Regarding function declarations, we can invoke them before actually declaring them like so:
sayHi(); // Hi function sayHi() { console.log('Hi'); };Function expressions, on the other hand, are not hoisted, so we’ll get the following error:
sayHi(); //Output: "TypeError: sayHi is not a function var sayHi = function() { console.log('Hi'); };ES6 introduced JavaScript developers the
letandconstkeywords. Whileletandconstare block-scoped and not function scoped asvarit shouldn’t make a difference while discussing their hoisting behavior. We’ll start from the end, JavaScript hoistsletandconst.console.log(hi); // Output: Cannot access 'hi' before initialization let hi = 'Hi';As we can see above,
letdoesn’t allow us to use undeclared variables, hence the interpreter explicitly output a reference error indicating that thehivariable cannot be accessed before initialization. The same error will occur if we change the abovelettoconstconsole.log(hi); // Output: Cannot access 'hi' before initialization const hi = 'Hi';So, bottom line, the JavaScript parser searches for variable declarations and functions and hoists them to the top of their scope before code execution and assign values to them in the memory so in case the interpreter will encounter them while executing the code he will recognize them and will be able to execute the code with their assigned values. Variables declared with
letorconstremain uninitialized at the beginning of execution while that variables declared withvarare being initialized with a value ofundefined.I added this visual illustration to better help understanding of how are the hoisted variables and function are being saved in the memory enter image description here
This explanation is taken from an article I wrote at Medium:
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope by the parser which reads the source code into an intermediate representation before the actual code execution starts by the JavaScript interpreter. So, it actually doesn’t matter where variables or functions are declared, they will be moved to the top of their scope regardless of whether their scope is global or local. This means that
console.log (hi); var hi = "say hi";is actually interpreted to
var hi = undefined; console.log (hi); hi = "say hi";So, as we saw just now,
varvariables are being hoisted to the top of their scope and are being initialized with the value of undefined which means that we can actually assign their value before actually declaring them in the code like so:hi = "say hi" console.log (hi); // say hi var hi;Regarding function declarations, we can invoke them before actually declaring them like so:
sayHi(); // Hi function sayHi() { console.log('Hi'); };Function expressions, on the other hand, are not hoisted, so we’ll get the following error:
sayHi(); //Output: "TypeError: sayHi is not a function var sayHi = function() { console.log('Hi'); };ES6 introduced JavaScript developers the
letandconstkeywords. Whileletandconstare block-scoped and not function scoped asvarit shouldn’t make a difference while discussing their hoisting behavior. We’ll start from the end, JavaScript hoistsletandconst.console.log(hi); // Output: Cannot access 'hi' before initialization let hi = 'Hi';As we can see above,
letdoesn’t allow us to use undeclared variables, hence the interpreter explicitly output a reference error indicating that thehivariable cannot be accessed before initialization. The same error will occur if we change the abovelettoconstconsole.log(hi); // Output: Cannot access 'hi' before initialization const hi = 'Hi';So, bottom line, the JavaScript parser searches for variable declarations and functions and hoists them to the top of their scope before code execution and assign values to them in the memory so in case the interpreter will encounter them while executing the code he will recognize them and will be able to execute the code with their assigned values. Variables declared with
letorconstremain uninitialized at the beginning of execution while that variables declared withvarare being initialized with a value ofundefined.I added this visual illustration to help understanding of how are the hoisted variables and function are being saved in the memory enter image description here
This explanation is taken from an article I wrote at Medium:
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope by the parser which reads the source code into an intermediate representation before the actual code execution starts by the JavaScript interpreter. So, it actually doesn’t matter where variables or functions are declared, they will be moved to the top of their scope regardless of whether their scope is global or local. This means that
console.log (hi); var hi = "say hi";is actually interpreted to
var hi = undefined; console.log (hi); hi = "say hi";So, as we saw just now,
varvariables are being hoisted to the top of their scope and are being initialized with the value of undefined which means that we can actually assign their value before actually declaring them in the code like so:hi = "say hi" console.log (hi); // say hi var hi;Regarding function declarations, we can invoke them before actually declaring them like so:
sayHi(); // Hi function sayHi() { console.log('Hi'); };Function expressions, on the other hand, are not hoisted, so we’ll get the following error:
sayHi(); //Output: "TypeError: sayHi is not a function var sayHi = function() { console.log('Hi'); };ES6 introduced JavaScript developers the
letandconstkeywords. Whileletandconstare block-scoped and not function scoped asvarit shouldn’t make a difference while discussing their hoisting behavior. We’ll start from the end, JavaScript hoistsletandconst.console.log(hi); // Output: Cannot access 'hi' before initialization let hi = 'Hi';As we can see above,
letdoesn’t allow us to use undeclared variables, hence the interpreter explicitly output a reference error indicating that thehivariable cannot be accessed before initialization. The same error will occur if we change the abovelettoconstconsole.log(hi); // Output: Cannot access 'hi' before initialization const hi = 'Hi';So, bottom line, the JavaScript parser searches for variable declarations and functions and hoists them to the top of their scope before code execution and assign values to them in the memory so in case the interpreter will encounter them while executing the code he will recognize them and will be able to execute the code with their assigned values. Variables declared with
letorconstremain uninitialized at the beginning of execution while that variables declared withvarare being initialized with a value ofundefined.I added this visual illustration to better help understanding of how are the hoisted variables and function are being saved in the memory enter image description here
- 21.3k
- 85
- 84
- 122