Why do variable in an inner function return nan when there is the same variable name at the inner function declared after log [duplicate] The Next CEO of Stack Overflow'Hoisted' JavaScript VariablesWhy does shadowed variable evaluate to undefined when defined in outside scope?What 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
Why is information "lost" when it got into a black hole?
Is there such a thing as a proper verb, like a proper noun?
Why don't programming languages automatically manage the synchronous/asynchronous problem?
Does the Idaho Potato Commission associate potato skins with healthy eating?
When "be it" is at the beginning of a sentence, what kind of structure do you call it?
Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?
How to find image of a complex function with given constraints?
Where do students learn to solve polynomial equations these days?
Is there a reasonable and studied concept of reduction between regular languages?
I dug holes for my pergola too wide
Does higher Oxidation/ reduction potential translate to higher energy storage in battery?
Is it ever safe to open a suspicious HTML file (e.g. email attachment)?
Can this note be analyzed as a non-chord tone?
AB diagonalizable then BA also diagonalizable
Is there a way to save my career from absolute disaster?
How do I fit a non linear curve?
How to set page number in right side in chapter title page?
My ex-girlfriend uses my Apple ID to login to her iPad, do I have to give her my Apple ID password to reset it?
(How) Could a medieval fantasy world survive a magic-induced "nuclear winter"?
What steps are necessary to read a Modern SSD in Medieval Europe?
Defamation due to breach of confidentiality
Would a completely good Muggle be able to use a wand?
How to avoid supervisors with prejudiced views?
Physiological effects of huge anime eyes
Why do variable in an inner function return nan when there is the same variable name at the inner function declared after log [duplicate]
The Next CEO of Stack Overflow'Hoisted' JavaScript VariablesWhy does shadowed variable evaluate to undefined when defined in outside scope?What 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
This question already has an answer here:
Why does shadowed variable evaluate to undefined when defined in outside scope?
6 answers
'Hoisted' JavaScript Variables
3 answers
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
marked as duplicate by Matsemann, Bergi
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does shadowed variable evaluate to undefined when defined in outside scope?
6 answers
'Hoisted' JavaScript Variables
3 answers
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
marked as duplicate by Matsemann, Bergi
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does shadowed variable evaluate to undefined when defined in outside scope?
6 answers
'Hoisted' JavaScript Variables
3 answers
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
This question already has an answer here:
Why does shadowed variable evaluate to undefined when defined in outside scope?
6 answers
'Hoisted' JavaScript Variables
3 answers
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
This question already has an answer here:
Why does shadowed variable evaluate to undefined when defined in outside scope?
6 answers
'Hoisted' JavaScript Variables
3 answers
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 yesterday
Nick Parsons
10.3k2926
10.3k2926
asked yesterday
ClaudeClaude
556
556
marked as duplicate by Matsemann, Bergi
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Matsemann, Bergi
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
4 Answers
4
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 |
Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this
function outer()
var a = 2;
function inner()
var a;
a++;
console.log(a) //log NaN
a = 8
inner()
outer()
you are trying to increment a undefined variable . This is the reason you are getting NaN error.
So if you initialize the variable after declaration(hoisting) then you will get the result.
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
yesterday
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
yesterday
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
yesterday
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
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 yesterday
Shidersz
9,3762933
9,3762933
answered yesterday
jrojro
604215
604215
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 yesterday
Jack BashfordJack Bashford
14.1k31848
14.1k31848
add a comment |
add a comment |
Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this
function outer()
var a = 2;
function inner()
var a;
a++;
console.log(a) //log NaN
a = 8
inner()
outer()
you are trying to increment a undefined variable . This is the reason you are getting NaN error.
So if you initialize the variable after declaration(hoisting) then you will get the result.
add a comment |
Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this
function outer()
var a = 2;
function inner()
var a;
a++;
console.log(a) //log NaN
a = 8
inner()
outer()
you are trying to increment a undefined variable . This is the reason you are getting NaN error.
So if you initialize the variable after declaration(hoisting) then you will get the result.
add a comment |
Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this
function outer()
var a = 2;
function inner()
var a;
a++;
console.log(a) //log NaN
a = 8
inner()
outer()
you are trying to increment a undefined variable . This is the reason you are getting NaN error.
So if you initialize the variable after declaration(hoisting) then you will get the result.
Javascript has the mechanism called hoisting .With the help of this mechanism javascript will automatically take all the variable definitions and kept it in the top of the function (whether it may be outer function or inner function). So in your case it will take only the variable definition and hoist it in the top . After hoisting you code will be like this
function outer()
var a = 2;
function inner()
var a;
a++;
console.log(a) //log NaN
a = 8
inner()
outer()
you are trying to increment a undefined variable . This is the reason you are getting NaN error.
So if you initialize the variable after declaration(hoisting) then you will get the result.
function outer()
var a = 2;
function inner()
var a;
a++;
console.log(a) //log NaN
a = 8
inner()
outer()
function outer()
var a = 2;
function inner()
var a;
a++;
console.log(a) //log NaN
a = 8
inner()
outer()
answered yesterday
SuRaSuRa
12114
12114
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
yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
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 yesterday
answered yesterday
Darshit ShahDarshit Shah
33
33
3
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
3
3
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
yesterday
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
yesterday
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
yesterday
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
yesterday
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
yesterday
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
yesterday
add a comment |