Function declarations and how they create variables in the current scope









up vote
1
down vote

favorite












I was reading this article about js functions.
https://dmitripavlutin.com/6-ways-to-declare-javascript-functions/



and it says "The function declaration creates a variable in the current scope with the identifier equal to function name. This variable holds the function object."



So I did some experiments to learn more.



function a () 
return 1;

console.log(typeof a === "function")
console.log(typeof a === "number")


This outputs

True

False



Which isn't surprising, then when I run this.



var a = 1;
function a ()
return 1;

console.log(typeof a === "function")
console.log(typeof a === "number")


The output is

False

True



So although a is allocated to a number and then later allocated to a function, it ends up being a number in the end.



Is there some kind of rule that says variable declarations override function declarations or is there more to it?










share|improve this question

















  • 1




    Maybe this is due to hoisting. I am not sure but that can be a reason. I am on phone so can't check. Please try replacing var with const. Let us know.
    – Akhil Gautam
    Nov 11 at 1:35














up vote
1
down vote

favorite












I was reading this article about js functions.
https://dmitripavlutin.com/6-ways-to-declare-javascript-functions/



and it says "The function declaration creates a variable in the current scope with the identifier equal to function name. This variable holds the function object."



So I did some experiments to learn more.



function a () 
return 1;

console.log(typeof a === "function")
console.log(typeof a === "number")


This outputs

True

False



Which isn't surprising, then when I run this.



var a = 1;
function a ()
return 1;

console.log(typeof a === "function")
console.log(typeof a === "number")


The output is

False

True



So although a is allocated to a number and then later allocated to a function, it ends up being a number in the end.



Is there some kind of rule that says variable declarations override function declarations or is there more to it?










share|improve this question

















  • 1




    Maybe this is due to hoisting. I am not sure but that can be a reason. I am on phone so can't check. Please try replacing var with const. Let us know.
    – Akhil Gautam
    Nov 11 at 1:35












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I was reading this article about js functions.
https://dmitripavlutin.com/6-ways-to-declare-javascript-functions/



and it says "The function declaration creates a variable in the current scope with the identifier equal to function name. This variable holds the function object."



So I did some experiments to learn more.



function a () 
return 1;

console.log(typeof a === "function")
console.log(typeof a === "number")


This outputs

True

False



Which isn't surprising, then when I run this.



var a = 1;
function a ()
return 1;

console.log(typeof a === "function")
console.log(typeof a === "number")


The output is

False

True



So although a is allocated to a number and then later allocated to a function, it ends up being a number in the end.



Is there some kind of rule that says variable declarations override function declarations or is there more to it?










share|improve this question













I was reading this article about js functions.
https://dmitripavlutin.com/6-ways-to-declare-javascript-functions/



and it says "The function declaration creates a variable in the current scope with the identifier equal to function name. This variable holds the function object."



So I did some experiments to learn more.



function a () 
return 1;

console.log(typeof a === "function")
console.log(typeof a === "number")


This outputs

True

False



Which isn't surprising, then when I run this.



var a = 1;
function a ()
return 1;

console.log(typeof a === "function")
console.log(typeof a === "number")


The output is

False

True



So although a is allocated to a number and then later allocated to a function, it ends up being a number in the end.



Is there some kind of rule that says variable declarations override function declarations or is there more to it?







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 1:31









h33

1968




1968







  • 1




    Maybe this is due to hoisting. I am not sure but that can be a reason. I am on phone so can't check. Please try replacing var with const. Let us know.
    – Akhil Gautam
    Nov 11 at 1:35












  • 1




    Maybe this is due to hoisting. I am not sure but that can be a reason. I am on phone so can't check. Please try replacing var with const. Let us know.
    – Akhil Gautam
    Nov 11 at 1:35







1




1




Maybe this is due to hoisting. I am not sure but that can be a reason. I am on phone so can't check. Please try replacing var with const. Let us know.
– Akhil Gautam
Nov 11 at 1:35




Maybe this is due to hoisting. I am not sure but that can be a reason. I am on phone so can't check. Please try replacing var with const. Let us know.
– Akhil Gautam
Nov 11 at 1:35












1 Answer
1






active

oldest

votes

















up vote
2
down vote













Function declarations are hoisted to the top of their containing function (or the outermost block). a will start out as contain



Your lower code is equivalent to the following:






var a = function a () 
return 1;

// next line reassigns `a` to number:
a = 1;
console.log(typeof a === "function")
console.log(typeof a === "number")





If you log a before the line a = 1, you'll see that it is indeed a function before it gets reassigned:






console.log(typeof a);
var a = 1;
console.log(typeof a);
function a ()
return 1;








share|improve this answer




















  • aren't variable hoisted as well? I guess function are just hoisted higher?
    – h33
    Nov 11 at 1:38










  • Variable creation is hoisted (eg var a;, doesn't make a difference here though), but variable assignment is not hoisted (eg a = 10;, assignment will always occur on the same line as you expect it to occur).
    – CertainPerformance
    Nov 11 at 1:39











  • ahh that makes sense. Thanks.
    – h33
    Nov 11 at 1:42










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245082%2ffunction-declarations-and-how-they-create-variables-in-the-current-scope%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













Function declarations are hoisted to the top of their containing function (or the outermost block). a will start out as contain



Your lower code is equivalent to the following:






var a = function a () 
return 1;

// next line reassigns `a` to number:
a = 1;
console.log(typeof a === "function")
console.log(typeof a === "number")





If you log a before the line a = 1, you'll see that it is indeed a function before it gets reassigned:






console.log(typeof a);
var a = 1;
console.log(typeof a);
function a ()
return 1;








share|improve this answer




















  • aren't variable hoisted as well? I guess function are just hoisted higher?
    – h33
    Nov 11 at 1:38










  • Variable creation is hoisted (eg var a;, doesn't make a difference here though), but variable assignment is not hoisted (eg a = 10;, assignment will always occur on the same line as you expect it to occur).
    – CertainPerformance
    Nov 11 at 1:39











  • ahh that makes sense. Thanks.
    – h33
    Nov 11 at 1:42














up vote
2
down vote













Function declarations are hoisted to the top of their containing function (or the outermost block). a will start out as contain



Your lower code is equivalent to the following:






var a = function a () 
return 1;

// next line reassigns `a` to number:
a = 1;
console.log(typeof a === "function")
console.log(typeof a === "number")





If you log a before the line a = 1, you'll see that it is indeed a function before it gets reassigned:






console.log(typeof a);
var a = 1;
console.log(typeof a);
function a ()
return 1;








share|improve this answer




















  • aren't variable hoisted as well? I guess function are just hoisted higher?
    – h33
    Nov 11 at 1:38










  • Variable creation is hoisted (eg var a;, doesn't make a difference here though), but variable assignment is not hoisted (eg a = 10;, assignment will always occur on the same line as you expect it to occur).
    – CertainPerformance
    Nov 11 at 1:39











  • ahh that makes sense. Thanks.
    – h33
    Nov 11 at 1:42












up vote
2
down vote










up vote
2
down vote









Function declarations are hoisted to the top of their containing function (or the outermost block). a will start out as contain



Your lower code is equivalent to the following:






var a = function a () 
return 1;

// next line reassigns `a` to number:
a = 1;
console.log(typeof a === "function")
console.log(typeof a === "number")





If you log a before the line a = 1, you'll see that it is indeed a function before it gets reassigned:






console.log(typeof a);
var a = 1;
console.log(typeof a);
function a ()
return 1;








share|improve this answer












Function declarations are hoisted to the top of their containing function (or the outermost block). a will start out as contain



Your lower code is equivalent to the following:






var a = function a () 
return 1;

// next line reassigns `a` to number:
a = 1;
console.log(typeof a === "function")
console.log(typeof a === "number")





If you log a before the line a = 1, you'll see that it is indeed a function before it gets reassigned:






console.log(typeof a);
var a = 1;
console.log(typeof a);
function a ()
return 1;








var a = function a () 
return 1;

// next line reassigns `a` to number:
a = 1;
console.log(typeof a === "function")
console.log(typeof a === "number")





var a = function a () 
return 1;

// next line reassigns `a` to number:
a = 1;
console.log(typeof a === "function")
console.log(typeof a === "number")





console.log(typeof a);
var a = 1;
console.log(typeof a);
function a ()
return 1;





console.log(typeof a);
var a = 1;
console.log(typeof a);
function a ()
return 1;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 1:33









CertainPerformance

67.1k143252




67.1k143252











  • aren't variable hoisted as well? I guess function are just hoisted higher?
    – h33
    Nov 11 at 1:38










  • Variable creation is hoisted (eg var a;, doesn't make a difference here though), but variable assignment is not hoisted (eg a = 10;, assignment will always occur on the same line as you expect it to occur).
    – CertainPerformance
    Nov 11 at 1:39











  • ahh that makes sense. Thanks.
    – h33
    Nov 11 at 1:42
















  • aren't variable hoisted as well? I guess function are just hoisted higher?
    – h33
    Nov 11 at 1:38










  • Variable creation is hoisted (eg var a;, doesn't make a difference here though), but variable assignment is not hoisted (eg a = 10;, assignment will always occur on the same line as you expect it to occur).
    – CertainPerformance
    Nov 11 at 1:39











  • ahh that makes sense. Thanks.
    – h33
    Nov 11 at 1:42















aren't variable hoisted as well? I guess function are just hoisted higher?
– h33
Nov 11 at 1:38




aren't variable hoisted as well? I guess function are just hoisted higher?
– h33
Nov 11 at 1:38












Variable creation is hoisted (eg var a;, doesn't make a difference here though), but variable assignment is not hoisted (eg a = 10;, assignment will always occur on the same line as you expect it to occur).
– CertainPerformance
Nov 11 at 1:39





Variable creation is hoisted (eg var a;, doesn't make a difference here though), but variable assignment is not hoisted (eg a = 10;, assignment will always occur on the same line as you expect it to occur).
– CertainPerformance
Nov 11 at 1:39













ahh that makes sense. Thanks.
– h33
Nov 11 at 1:42




ahh that makes sense. Thanks.
– h33
Nov 11 at 1:42

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245082%2ffunction-declarations-and-how-they-create-variables-in-the-current-scope%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党