Do you really know about Hoisting?

function hoist() {
a = 20;
var b = 100;
}

console.log(a); // error a is not defined

hoist();

console.log(a); // 20 a becomes global variable
console.log(b); // error b is not defined (it is defined in hoist method scope not global)
console.log(x); // undefined
var x = 10;
var x;
console.log(x);
x = 10;
function getName() {
console.log(name);
var name = "Max";
}

getName(); // undefined
function getName() {
var name = "Max";
console.log(name);
}

getName(); // Max
console.log(age); // Output: ReferenceError: age is not defined ...
let age = 25;
let age;
console.log(age); // undefined
age = 25;
console.log(age); // Output: ReferenceError: age is not defined ...
const age = 25;
const age;  // Missing initializer in const declaration
function print(var name) {
console.log(name);
}
print("Alax");  // Alax

function print(var name) {
console.log(name);
}
let printFn = function(var name) {
console.log(name);
}
printFn();   //Output: "printFn: expression is not a function

let printFn = function(var name) {
console.log(name);
}
var name = "Tom";

function name() {
console.log("Harry");
}

console.log(typeof name); // string (variable assignment take precedence)
// 2nd Examplevar name;

function name() {
console.log("Harry");
}

console.log(typeof name); // function (function declaration take precedence)
var name = fullName();
name.firstName = "Sourav";
name.lastName = "Ganguly";
console.log(name); // Reference error!!!

class fullName {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
class fullName {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}

var name = fullName();
name.firstName = "Sourav";
name.lastName = "Ganguly";
console.log(name); // Output {firstName: "Sourav", lastName: "Ganguly"}
var name = getFullName();
name.firstName = "Sourav";
name.lastName = "Ganguly";
console.log(name); // TypeError!!! getFullName is not a constructor

var getFullName = class fullName {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
};

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store