Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their containing
scope during the compile phase, before the code has been executed. It's important to note that only the declarations are
hoisted, not initializations. If a variable is declared and initialized after using it, the variable will be undefined.
For example,
console.log(myVar); // Output: undefined
var myVar = 5;
console.log(myVar); // Output: 5
Here,
myVar declaration is hoisted to the top, but its initialization with the value of 5 is not, leading to undefined when first logged.
Learn more about hoisting
in this book.
In JavaScript, undefined means a variable has been declared but has not yet been assigned a value. On the other hand,
null is an assignment value that represents no value or no object. It implies an empty or non-existent value. It's
important to note that JavaScript treats undefined and null as two distinct types: typeof undefined returns undefined,
whereas typeof null returns object. Learn more about JavaScript terminology in this book.
A Promise in JavaScript is an object representing the eventual completion or failure of an asynchronous operation. It
serves as a link between the function executing the asynchronous operation (the promise "producer") and the functions
which will handle the result of that operation (the promise "consumers").
The Promise object can be in one of three
states: pending (the initial state), fulfilled (operation completed successfully), or rejected (operation failed).
Promises are used to handle asynchronous operations, and they provide a more flexible and powerful way of handling
asynchronous code than traditional callback functions, reducing the risk of ending up in "callback hell". Learn more about JavaScript Promises
and "callback hell" in this book.