30
loading...
This website collects cookies to deliver better user experience
console.log('myName',myName);
var myName = 'JavaScript';
console.log('myName',myName);
undefined
and second one will be Javascript
.var myName -> Declaration
myName = 'JavaScript' -> Initialization
console.log('myName',myName); -> Utilization.
undefined
and in the second run while running through all the code it will assign the actual value for it.let
and const
hoisted a bit different.Let
and const
are hoisted in block scope whereas var
is hoisted in global scope. (Scope is another important concept which we will discuss in future post).console.log('myName',myName);
let myName = 'Javascript';
let
or const
) and initializing the variable./*
let myName;
//Beginning of the temporal dead zone
console.log(firstname); // ReferenceError as accessed in the TDZ.
let myName = 'Javascript'; // Ending of the temporal dead zone
*/
Let
and const
is recommended because unlike var
, there’s no risk of variable leakage outside of the scope of execution unless if needed. To learn more about var,let and const declarations, Please refer this linkgreetings();
function greetings(){
console.log('Hello from dev community')
}
greetings();
function greetings(){
console.log('First',message);
var message = 'Hello from Dev Community';
console.log('Second',message);
}
undefined
because variables declared inside functions will be hoisted only top of the particular scope (code blocks). So the code will begreetings();
function greetings(){
var message;
console.log('First',message);
message = 'Hello from Dev Community';
console.log('Second',message);
}
greetings(); // Ouput: TypeError: expression is not a function.
var greetings = function hoisting() {
console.log('Hello from function expression?');
};
TypeError
because unlike function declaration, only the variable was hoisted. When variables declared with var are hoisted, they are given a default value of undefined
. JavaScript then throws an error because the value of the variable is not a function at that point of time.greetings(); // Ouput: TypeError: expression is not a function.
const greetings = () => {
console.log('Hello from arrow functions?');
};
var abc;
function abc(){}
console.log(typeof abc)
function abcd(){}
var abcd
console.log(typeof abcd)