Why do variable in an inner function return nan when there is the same variable name at the inner function declared after log The Next CEO of Stack OverflowWhat is the naming convention in Python for variable and function names?How to execute a JavaScript function when I have its name as a stringWhat is a practical use for a closure in JavaScript?Javascript by reference vs. by valueWhy aren't ◎ܫ◎ and ☺ valid JavaScript variable names?What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?Is the recommendation to include CSS before JavaScript invalid?variable not writable in inner functionjavascript variable returning NaNFunction returns NaN when it shouldn't
Multiple labels for a single equation
Should I tutor a student who I know has cheated on their homework?
How to avoid supervisors with prejudiced views?
How did people program for Consoles with multiple CPUs?
Several mode to write the symbol of a vector
If/When UK leaves the EU, can a future goverment conduct a referendum to join the EU?
How to invert MapIndexed on a ragged structure? How to construct a tree from rules?
Novel about a guy who is possessed by the divine essence and the world ends?
What connection does MS Office have to Netscape Navigator?
Why am I allowed to create multiple unique pointers from a single object?
Geometry problem - areas of triangles (contest math)
Are there any limitations on attacking while grappling?
Grabbing quick drinks
What benefits would be gained by using human laborers instead of drones in deep sea mining?
How do I go from 300 unfinished/half written blog posts, to published posts?
How to start emacs in "nothing" mode (`fundamental-mode`)
What is the purpose of the Evocation wizard's Potent Cantrip feature?
Preparing Indesign booklet with .psd graphics for print
Inappropriate reference requests from Journal reviewers
Does it take more energy to get to Venus or to Mars?
What happens if you roll doubles 3 times then land on "Go to jail?"
Indicator light circuit
Received an invoice from my ex-employer billing me for training; how to handle?
Contours of a clandestine nature
Why do variable in an inner function return nan when there is the same variable name at the inner function declared after log
The Next CEO of Stack OverflowWhat is the naming convention in Python for variable and function names?How to execute a JavaScript function when I have its name as a stringWhat is a practical use for a closure in JavaScript?Javascript by reference vs. by valueWhy aren't ◎ܫ◎ and ☺ valid JavaScript variable names?What is the explanation for these bizarre JavaScript behaviours mentioned in the 'Wat' talk for CodeMash 2012?Is the recommendation to include CSS before JavaScript invalid?variable not writable in inner functionjavascript variable returning NaNFunction returns NaN when it shouldn't
What's happening here? I get a different result if I declare a variable after console.log
in the inner function
I understand that var has a functional scope and inner function can access the variable from their parent
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log NaN
var a = 8
inner()
outer()
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log 3
var b = 8
inner()
outer()
The log returns NaN
in the first example and log 3
in the second example
javascript function
add a comment |
What's happening here? I get a different result if I declare a variable after console.log
in the inner function
I understand that var has a functional scope and inner function can access the variable from their parent
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log NaN
var a = 8
inner()
outer()
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log 3
var b = 8
inner()
outer()
The log returns NaN
in the first example and log 3
in the second example
javascript function
add a comment |
What's happening here? I get a different result if I declare a variable after console.log
in the inner function
I understand that var has a functional scope and inner function can access the variable from their parent
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log NaN
var a = 8
inner()
outer()
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log 3
var b = 8
inner()
outer()
The log returns NaN
in the first example and log 3
in the second example
javascript function
What's happening here? I get a different result if I declare a variable after console.log
in the inner function
I understand that var has a functional scope and inner function can access the variable from their parent
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log NaN
var a = 8
inner()
outer()
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log 3
var b = 8
inner()
outer()
The log returns NaN
in the first example and log 3
in the second example
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log NaN
var a = 8
inner()
outer()
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log NaN
var a = 8
inner()
outer()
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log 3
var b = 8
inner()
outer()
function outer()
var a = 2;
function inner()
a++;
console.log(a) //log 3
var b = 8
inner()
outer()
javascript function
javascript function
edited 1 hour ago
Nick Parsons
10.3k2926
10.3k2926
asked 1 hour ago
ClaudeClaude
476
476
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
This is due to hoisting
The declaration of a
in the inner function is hoisted to the top of the function, overriding the outer function's a
, so a
is undefined
undefined++
returns NaN
, hence your result.
Your code is equivalent to:
function outer()
var a=2;
function inner()
var a;
a++;
console.log(a); //log NaN
a = 8;
inner();
outer();
Rewriting your code in this way makes it easy to see what's going on.
add a comment |
Because var
is hoisted through the function, you're essentially running undefined++
which is NaN
. If you remove var a = 8
in inner
, the code works as expected:
function outer()
var a = 2;
function inner()
a++;
console.log(a);
inner();
outer();
add a comment |
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
var a=0;
function outer()
a=2;
function inner()
a=a+1;
console.log(a)
a = 8
inner()
outer()
3
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
1 hour ago
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
– Darshit Shah
1 hour ago
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
– double-beep
41 mins ago
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55428371%2fwhy-do-variable-in-an-inner-function-return-nan-when-there-is-the-same-variable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is due to hoisting
The declaration of a
in the inner function is hoisted to the top of the function, overriding the outer function's a
, so a
is undefined
undefined++
returns NaN
, hence your result.
Your code is equivalent to:
function outer()
var a=2;
function inner()
var a;
a++;
console.log(a); //log NaN
a = 8;
inner();
outer();
Rewriting your code in this way makes it easy to see what's going on.
add a comment |
This is due to hoisting
The declaration of a
in the inner function is hoisted to the top of the function, overriding the outer function's a
, so a
is undefined
undefined++
returns NaN
, hence your result.
Your code is equivalent to:
function outer()
var a=2;
function inner()
var a;
a++;
console.log(a); //log NaN
a = 8;
inner();
outer();
Rewriting your code in this way makes it easy to see what's going on.
add a comment |
This is due to hoisting
The declaration of a
in the inner function is hoisted to the top of the function, overriding the outer function's a
, so a
is undefined
undefined++
returns NaN
, hence your result.
Your code is equivalent to:
function outer()
var a=2;
function inner()
var a;
a++;
console.log(a); //log NaN
a = 8;
inner();
outer();
Rewriting your code in this way makes it easy to see what's going on.
This is due to hoisting
The declaration of a
in the inner function is hoisted to the top of the function, overriding the outer function's a
, so a
is undefined
undefined++
returns NaN
, hence your result.
Your code is equivalent to:
function outer()
var a=2;
function inner()
var a;
a++;
console.log(a); //log NaN
a = 8;
inner();
outer();
Rewriting your code in this way makes it easy to see what's going on.
edited 1 hour ago
Shidersz
9,3362933
9,3362933
answered 1 hour ago
jrojro
584115
584115
add a comment |
add a comment |
Because var
is hoisted through the function, you're essentially running undefined++
which is NaN
. If you remove var a = 8
in inner
, the code works as expected:
function outer()
var a = 2;
function inner()
a++;
console.log(a);
inner();
outer();
add a comment |
Because var
is hoisted through the function, you're essentially running undefined++
which is NaN
. If you remove var a = 8
in inner
, the code works as expected:
function outer()
var a = 2;
function inner()
a++;
console.log(a);
inner();
outer();
add a comment |
Because var
is hoisted through the function, you're essentially running undefined++
which is NaN
. If you remove var a = 8
in inner
, the code works as expected:
function outer()
var a = 2;
function inner()
a++;
console.log(a);
inner();
outer();
Because var
is hoisted through the function, you're essentially running undefined++
which is NaN
. If you remove var a = 8
in inner
, the code works as expected:
function outer()
var a = 2;
function inner()
a++;
console.log(a);
inner();
outer();
function outer()
var a = 2;
function inner()
a++;
console.log(a);
inner();
outer();
function outer()
var a = 2;
function inner()
a++;
console.log(a);
inner();
outer();
answered 1 hour ago
Jack BashfordJack Bashford
13.8k31848
13.8k31848
add a comment |
add a comment |
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
var a=0;
function outer()
a=2;
function inner()
a=a+1;
console.log(a)
a = 8
inner()
outer()
3
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
1 hour ago
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
– Darshit Shah
1 hour ago
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
– double-beep
41 mins ago
add a comment |
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
var a=0;
function outer()
a=2;
function inner()
a=a+1;
console.log(a)
a = 8
inner()
outer()
3
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
1 hour ago
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
– Darshit Shah
1 hour ago
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
– double-beep
41 mins ago
add a comment |
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
var a=0;
function outer()
a=2;
function inner()
a=a+1;
console.log(a)
a = 8
inner()
outer()
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
var a=0;
function outer()
a=2;
function inner()
a=a+1;
console.log(a)
a = 8
inner()
outer()
edited 38 mins ago
answered 1 hour ago
Darshit ShahDarshit Shah
53
53
3
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
1 hour ago
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
– Darshit Shah
1 hour ago
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
– double-beep
41 mins ago
add a comment |
3
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
1 hour ago
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
– Darshit Shah
1 hour ago
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
– double-beep
41 mins ago
3
3
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
1 hour ago
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
1 hour ago
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
– Darshit Shah
1 hour ago
They can’t access the inner function value so we have to defined globally. After globally you can use A value anywhere in the code
– Darshit Shah
1 hour ago
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
– double-beep
41 mins ago
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
– double-beep
41 mins ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f55428371%2fwhy-do-variable-in-an-inner-function-return-nan-when-there-is-the-same-variable%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