MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/onix79/javascript_function_facts/h5spkvt/?context=9999
r/learnjavascript • u/hsnice • Jul 19 '21
27 comments sorted by
View all comments
20
function declarations aren't the only thing that is hoisted, variable declarations with var are also hoisted.
var
e.g. the following does not work:
console.log('hello', future()); const future = function() { return ' world'; }
1 u/[deleted] Jul 19 '21 Does using var have a higher cost than using let? Does hoisting have a cost? 2 u/[deleted] Jul 19 '21 the only difference is, that you can't redeclare a let/const variable and that var variable are hoisted to the top. There is no cost. 2 u/[deleted] Jul 19 '21 You can redeclare let. I thought the only difference was scoping. 4 u/[deleted] Jul 19 '21 Reassign, yes, but not redeclare. let assignment = 3; assignment = true; let declaring = function() {} let declaring = false; // here will be a redeclaration error -5 u/[deleted] Jul 19 '21 We’ll that’s not really redeclaration is it? You’re assigning 2 variables with the same name? 1 u/[deleted] Jul 19 '21 well it is. the first declaring would be of type 'function' while the second declaring would be a boolean. you can do that in var : var a = function() {}; typeof a // "function" var a = false; typeof a // "boolean" 3 u/[deleted] Jul 19 '21 Interesting. Thanks for bringing this up!
1
Does using var have a higher cost than using let? Does hoisting have a cost?
2 u/[deleted] Jul 19 '21 the only difference is, that you can't redeclare a let/const variable and that var variable are hoisted to the top. There is no cost. 2 u/[deleted] Jul 19 '21 You can redeclare let. I thought the only difference was scoping. 4 u/[deleted] Jul 19 '21 Reassign, yes, but not redeclare. let assignment = 3; assignment = true; let declaring = function() {} let declaring = false; // here will be a redeclaration error -5 u/[deleted] Jul 19 '21 We’ll that’s not really redeclaration is it? You’re assigning 2 variables with the same name? 1 u/[deleted] Jul 19 '21 well it is. the first declaring would be of type 'function' while the second declaring would be a boolean. you can do that in var : var a = function() {}; typeof a // "function" var a = false; typeof a // "boolean" 3 u/[deleted] Jul 19 '21 Interesting. Thanks for bringing this up!
2
the only difference is, that you can't redeclare a let/const variable and that var variable are hoisted to the top. There is no cost.
let
const
2 u/[deleted] Jul 19 '21 You can redeclare let. I thought the only difference was scoping. 4 u/[deleted] Jul 19 '21 Reassign, yes, but not redeclare. let assignment = 3; assignment = true; let declaring = function() {} let declaring = false; // here will be a redeclaration error -5 u/[deleted] Jul 19 '21 We’ll that’s not really redeclaration is it? You’re assigning 2 variables with the same name? 1 u/[deleted] Jul 19 '21 well it is. the first declaring would be of type 'function' while the second declaring would be a boolean. you can do that in var : var a = function() {}; typeof a // "function" var a = false; typeof a // "boolean" 3 u/[deleted] Jul 19 '21 Interesting. Thanks for bringing this up!
You can redeclare let. I thought the only difference was scoping.
4 u/[deleted] Jul 19 '21 Reassign, yes, but not redeclare. let assignment = 3; assignment = true; let declaring = function() {} let declaring = false; // here will be a redeclaration error -5 u/[deleted] Jul 19 '21 We’ll that’s not really redeclaration is it? You’re assigning 2 variables with the same name? 1 u/[deleted] Jul 19 '21 well it is. the first declaring would be of type 'function' while the second declaring would be a boolean. you can do that in var : var a = function() {}; typeof a // "function" var a = false; typeof a // "boolean" 3 u/[deleted] Jul 19 '21 Interesting. Thanks for bringing this up!
4
Reassign, yes, but not redeclare.
let assignment = 3; assignment = true; let declaring = function() {} let declaring = false; // here will be a redeclaration error
-5 u/[deleted] Jul 19 '21 We’ll that’s not really redeclaration is it? You’re assigning 2 variables with the same name? 1 u/[deleted] Jul 19 '21 well it is. the first declaring would be of type 'function' while the second declaring would be a boolean. you can do that in var : var a = function() {}; typeof a // "function" var a = false; typeof a // "boolean" 3 u/[deleted] Jul 19 '21 Interesting. Thanks for bringing this up!
-5
We’ll that’s not really redeclaration is it? You’re assigning 2 variables with the same name?
1 u/[deleted] Jul 19 '21 well it is. the first declaring would be of type 'function' while the second declaring would be a boolean. you can do that in var : var a = function() {}; typeof a // "function" var a = false; typeof a // "boolean" 3 u/[deleted] Jul 19 '21 Interesting. Thanks for bringing this up!
well it is. the first declaring would be of type 'function' while the second declaring would be a boolean. you can do that in var :
declaring
boolean
var a = function() {}; typeof a // "function" var a = false; typeof a // "boolean"
3 u/[deleted] Jul 19 '21 Interesting. Thanks for bringing this up!
3
Interesting. Thanks for bringing this up!
20
u/[deleted] Jul 19 '21
function declarations aren't the only thing that is hoisted, variable declarations with
varare also hoisted.e.g. the following does not work: