4

Here is my code, I am working on a Node.js application, I would like to define a variable inside a function then be able to use its value out side the function too, how can I achieve this?

var readline = require('readline');
var rl = readline.createInterface(process.stdin, process.stdout);
rl.setPrompt('guess> ');
rl.prompt();
rl.on('line', function (line) {
    inputl = line; //I belive not using Var makes the variable Global? am
    am I correct in thinking so?
            if (line === "correct")
        console.log('correct answer my    freind.')//rl.close();
    if (line === 'quit')
        rl.close();
    rl.prompt();
}).on('close', function () {
    process.exit(0);
});
{
    console.log(inputl);
}

Thank you for your help, I have switched to sync.prompt, which has now fixed the issue. Your answers did help in my understanding of js, so they did serve a purpose.

alex tix
  • 175
  • 2
  • 7
  • Why define it inside of the function? You can declare it outside of the function then assign a value to it inside of the function. Just know that the value will be `undefined` when you make that first `console.log` call at the bottom. – Mike Cluck Feb 02 '16 at 20:11
  • Because I can't do it outside. – alex tix Feb 02 '16 at 20:13
  • Console.log calls at the bottom just to show if my variable was being defined, its there purely for me. – alex tix Feb 02 '16 at 20:13
  • Why can't you define it outside? Just place `var inputl` at the top of your script. – Mike Cluck Feb 02 '16 at 20:14
  • Because the way I did it just doesn't work if I define it outside. – alex tix Feb 02 '16 at 20:17
  • Well, you need to rewrite it according to ECMA-262. :) – Microfed Feb 02 '16 at 20:18
  • 1
    That doesn't make sense. Show the code that "doesn't work" if you define it outside. Also, define "doesn't work". – Mike Cluck Feb 02 '16 at 20:19
  • I get, line is not defined. but line is a variable inside the function, or at least it looks like it? I am starting to learn node.js and js. – alex tix Feb 02 '16 at 20:22
  • You can't access `line` outside of the `rl.on('line', function` because it's a parameter passed *to* that function. If you declare `inputl` outside of the function you could assign it the value of `line` but it will have the value of `undefined` until that callback is called. You're dealing with asynchronous code. – Mike Cluck Feb 02 '16 at 20:25

2 Answers2

1

In nodejs var varName within a module defines a variable local to the module. Ref. This is different from the way javascript behaves in a browser.

If you must define a global variable from within the function, you can use global.input1 = line; in node (same reference as above).

traktor
  • 17,588
  • 4
  • 32
  • 53
0

EDITED

The top level declaration for Node is:

var yourVar = '';

There is no other global that would be reachable when declared inside the function.

More about this can be read HERE

Zak
  • 6,976
  • 2
  • 26
  • 48
  • yup, is there a node alternative? – alex tix Feb 02 '16 at 20:16
  • Touche .. Then the only way to do it is declare outside the function. `var something;` – Zak Feb 02 '16 at 20:16
  • Can I set a function as the value of a variable? like var myvar = function(myfunction); – alex tix Feb 02 '16 at 20:19
  • okay, how can I fix my code so that I can set the value of a variable to line, like inside the function? new to javascript. – alex tix Feb 02 '16 at 20:21
  • I suggest you read here .. You may have to rewrite the function. https://nodejs.org/en/docs/es6/ .. Again, `Node` may not be what you're looking to use if you MUST declare your variable there ... – Zak Feb 02 '16 at 20:24
  • @Zak Why should they look there? And it doesn't matter if they're using Node or running this code in a browser. Javascripts semantics work the same regardless of the environment. – Mike Cluck Feb 02 '16 at 20:27
  • He is asking for the variable to be available outside the module ... This, according to the documentation, isn't possible ... I may be wrong, but according to the docs ... Variables declared inside the module, stay inside the module. – Zak Feb 02 '16 at 20:31
  • @Zak 1. The ES6 features page doesn't say that (but it is true). 2. Nothing OP said indicates they want to use it outside of the module. 3. [Node does have global variables](http://stackoverflow.com/questions/5447771/node-js-global-variables) but as with any programming language, should be avoided. – Mike Cluck Feb 02 '16 at 20:38