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?
javascript
add a comment |
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?
javascript
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 replacingvar
withconst
. Let us know.
– Akhil Gautam
Nov 11 at 1:35
add a comment |
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?
javascript
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
javascript
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 replacingvar
withconst
. Let us know.
– Akhil Gautam
Nov 11 at 1:35
add a comment |
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 replacingvar
withconst
. 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
add a comment |
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;
aren't variable hoisted as well? I guess function are just hoisted higher?
– h33
Nov 11 at 1:38
Variable creation is hoisted (egvar a;
, doesn't make a difference here though), but variable assignment is not hoisted (ega = 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
add a comment |
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;
aren't variable hoisted as well? I guess function are just hoisted higher?
– h33
Nov 11 at 1:38
Variable creation is hoisted (egvar a;
, doesn't make a difference here though), but variable assignment is not hoisted (ega = 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
add a comment |
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;
aren't variable hoisted as well? I guess function are just hoisted higher?
– h33
Nov 11 at 1:38
Variable creation is hoisted (egvar a;
, doesn't make a difference here though), but variable assignment is not hoisted (ega = 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
add a comment |
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;
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;
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 (egvar a;
, doesn't make a difference here though), but variable assignment is not hoisted (ega = 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
add a comment |
aren't variable hoisted as well? I guess function are just hoisted higher?
– h33
Nov 11 at 1:38
Variable creation is hoisted (egvar a;
, doesn't make a difference here though), but variable assignment is not hoisted (ega = 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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
withconst
. Let us know.– Akhil Gautam
Nov 11 at 1:35