Javascript global variable undeclared problem











up vote
0
down vote

favorite












In the mentioned sample code I'm trying to get a value for val variable. I declared it with global scope. And add some console log to verify values. But inside of the function it assign value without problem. But out of the function val is undeclared. why is that?



$.validator.addMethod("serialverify", function(){
var val;
$("#serialno").keyup(function(){
serial().done(function(data){
console.log('final = ' + data);
val = data;
console.log(val);
});
}); console.log(val);
return val;
}, "Please enter valid serial code");









share|improve this question


















  • 2




    "I declared it with global scope." No you didn't.
    – Robby Cornelissen
    Nov 19 at 7:10






  • 1




    doesn't seem like your val variable is global
    – Alon Yampolski
    Nov 19 at 7:12










  • return val will always produce null as your assignment is taking place in a presumably asynchronous callback serial().done()
    – Abana Clara
    Nov 19 at 7:17















up vote
0
down vote

favorite












In the mentioned sample code I'm trying to get a value for val variable. I declared it with global scope. And add some console log to verify values. But inside of the function it assign value without problem. But out of the function val is undeclared. why is that?



$.validator.addMethod("serialverify", function(){
var val;
$("#serialno").keyup(function(){
serial().done(function(data){
console.log('final = ' + data);
val = data;
console.log(val);
});
}); console.log(val);
return val;
}, "Please enter valid serial code");









share|improve this question


















  • 2




    "I declared it with global scope." No you didn't.
    – Robby Cornelissen
    Nov 19 at 7:10






  • 1




    doesn't seem like your val variable is global
    – Alon Yampolski
    Nov 19 at 7:12










  • return val will always produce null as your assignment is taking place in a presumably asynchronous callback serial().done()
    – Abana Clara
    Nov 19 at 7:17













up vote
0
down vote

favorite









up vote
0
down vote

favorite











In the mentioned sample code I'm trying to get a value for val variable. I declared it with global scope. And add some console log to verify values. But inside of the function it assign value without problem. But out of the function val is undeclared. why is that?



$.validator.addMethod("serialverify", function(){
var val;
$("#serialno").keyup(function(){
serial().done(function(data){
console.log('final = ' + data);
val = data;
console.log(val);
});
}); console.log(val);
return val;
}, "Please enter valid serial code");









share|improve this question













In the mentioned sample code I'm trying to get a value for val variable. I declared it with global scope. And add some console log to verify values. But inside of the function it assign value without problem. But out of the function val is undeclared. why is that?



$.validator.addMethod("serialverify", function(){
var val;
$("#serialno").keyup(function(){
serial().done(function(data){
console.log('final = ' + data);
val = data;
console.log(val);
});
}); console.log(val);
return val;
}, "Please enter valid serial code");






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 7:09









Adam

165




165








  • 2




    "I declared it with global scope." No you didn't.
    – Robby Cornelissen
    Nov 19 at 7:10






  • 1




    doesn't seem like your val variable is global
    – Alon Yampolski
    Nov 19 at 7:12










  • return val will always produce null as your assignment is taking place in a presumably asynchronous callback serial().done()
    – Abana Clara
    Nov 19 at 7:17














  • 2




    "I declared it with global scope." No you didn't.
    – Robby Cornelissen
    Nov 19 at 7:10






  • 1




    doesn't seem like your val variable is global
    – Alon Yampolski
    Nov 19 at 7:12










  • return val will always produce null as your assignment is taking place in a presumably asynchronous callback serial().done()
    – Abana Clara
    Nov 19 at 7:17








2




2




"I declared it with global scope." No you didn't.
– Robby Cornelissen
Nov 19 at 7:10




"I declared it with global scope." No you didn't.
– Robby Cornelissen
Nov 19 at 7:10




1




1




doesn't seem like your val variable is global
– Alon Yampolski
Nov 19 at 7:12




doesn't seem like your val variable is global
– Alon Yampolski
Nov 19 at 7:12












return val will always produce null as your assignment is taking place in a presumably asynchronous callback serial().done()
– Abana Clara
Nov 19 at 7:17




return val will always produce null as your assignment is taking place in a presumably asynchronous callback serial().done()
– Abana Clara
Nov 19 at 7:17












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.



However, the statement return val is happening synchronously.



Suggestion -



You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -



......done( function(data) { ..... resolve(val); })


or declare var val; as global that is outside, the addMethod function






share|improve this answer





















  • oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
    – Adam
    Nov 19 at 7:24











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%2f53369835%2fjavascript-global-variable-undeclared-problem%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
3
down vote



accepted










There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.



However, the statement return val is happening synchronously.



Suggestion -



You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -



......done( function(data) { ..... resolve(val); })


or declare var val; as global that is outside, the addMethod function






share|improve this answer





















  • oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
    – Adam
    Nov 19 at 7:24















up vote
3
down vote



accepted










There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.



However, the statement return val is happening synchronously.



Suggestion -



You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -



......done( function(data) { ..... resolve(val); })


or declare var val; as global that is outside, the addMethod function






share|improve this answer





















  • oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
    – Adam
    Nov 19 at 7:24













up vote
3
down vote



accepted







up vote
3
down vote



accepted






There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.



However, the statement return val is happening synchronously.



Suggestion -



You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -



......done( function(data) { ..... resolve(val); })


or declare var val; as global that is outside, the addMethod function






share|improve this answer












There is a fundamental problem in your code. The assignment is taking place in a callback function, which means it'll only execute when the action keyup is made.



However, the statement return val is happening synchronously.



Suggestion -



You can wrap it in a promise of some sort and resolve it when done is executed. Something on the lines of -



......done( function(data) { ..... resolve(val); })


or declare var val; as global that is outside, the addMethod function







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 7:14









Aseem Upadhyay

1,056621




1,056621












  • oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
    – Adam
    Nov 19 at 7:24


















  • oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
    – Adam
    Nov 19 at 7:24
















oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
– Adam
Nov 19 at 7:24




oh. I'm stupid.. @Aseem thanks a lot friend.. now I got it.
– Adam
Nov 19 at 7:24


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369835%2fjavascript-global-variable-undeclared-problem%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