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
Shortening a title without changing its meaning
How can I separate the number from the unit in argument?
Simplify trigonometric expression using trigonometric identities
Why doesn't Shulchan Aruch include the laws of destroying fruit trees?
Is it possible to make a 9x9 table fit within the default margins?
How to show a landlord what we have in savings?
How do I secure a TV wall mount?
Salesforce opportunity stages
Could a dragon use its wings to swim?
Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?
How should I connect my cat5 cable to connectors having an orange-green line?
Horror film about a man brought out of cryogenic suspension without a soul, around 1990
What day is it again?
Creating a script with console commands
"Eavesdropping" vs "Listen in on"
Read/write a pipe-delimited file line by line with some simple text manipulation
How to pronounce fünf in 45
How can the PCs determine if an item is a phylactery?
Is it okay to majorly distort historical facts while writing a fiction story?
How exploitable/balanced is this homebrew spell: Spell Permanency?
Free fall ellipse or parabola?
Calculate the Mean mean of two numbers
Does Germany produce more waste than the US?
Is the offspring between a demon and a celestial possible? If so what is it called and is it in a book somewhere?
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();
);
);
);
2 days ago
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();
);
);
);
2 days ago
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 2 days ago
Nick Parsons
10.4k2926
10.4k2926
asked 2 days ago
ClaudeClaude
586
586
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();
);
);
);
2 days ago
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();
);
);
);
2 days ago
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
2 days 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
2 days 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
2 days ago
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 2 days ago
Shidersz
9,4382933
9,4382933
answered 2 days ago
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 2 days ago
Jack BashfordJack Bashford
14.4k31848
14.4k31848
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 2 days ago
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
2 days 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
2 days 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
2 days 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
2 days 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
2 days 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
2 days 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 2 days ago
answered 2 days ago
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
2 days 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
2 days 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
2 days 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
2 days 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
2 days 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
2 days ago
3
3
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
2 days ago
How does this piece of code explains the issue? Can you provide an explanation of the code you have posted?
– Shidersz
2 days 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
2 days 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
2 days 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
2 days 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
2 days ago
add a comment |