0

This is a basic vs code question, but I'm kinda new to this code editor. I need to run/ console.log() a simple js script in vs code platform same as what works in the browser's console or this "code snippet". Just for example of my simple js script:

const data = parseInt(prompt("5 x 5?"));
const testResult = (answer) => data === answer ? console.log(`Correct. Your answer was: ${data}`) : console.log(`Incorrect. Your answer was: ${data}. Pls try again`)

testResult(25);

If I console.log() this in the browser, it looks something like this. A prompts pops up and you can enter the value and see the result on the log.

enter image description here

enter image description here

How do I do this in Vs code terminal instead of going to a browser's console, run and see the result? I tried to run it for example node test.js in vs code terminal. Of course, it's getting an error, because prompt is not defined. I've seen and tried browser preview extension, but doesn't work for a simple code like this. A json script for a setting maybe or any extension that works? Thanks for your help.

2 Answers 2

0

As we know node.js code run outside of browser so you will see slightly difference between vanilla JavaScript and nodeJs.

In nodeJS for taking input from users you need to readline from terminal as we do in C++ or JAVA code

Example

const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("What is your name ? ", function(name) {
    rl.question("Where do you live ? ", function(country) {
        console.log(`${name}, is a citizen of ${country}`);
        rl.close();
    });
});

rl.on("close", function() {
    console.log("\nBYE BYE !!!");
    process.exit(0);
});

run above code using node command

0

Vanilla JavaScript and Node.js have some differences and this is one of them. You have some options:

  • You can use the built-in readline module
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

rl.question("How old are you ? ", function(age) {
        console.log(`You are ${age} years old`);
        rl.close();
    });
});

rl.on("close", function() {
    process.exit(0);
});

Reference: https://nodejs.org/en/knowledge/command-line/how-to-prompt-for-command-line-input/

  • You can use your package manager to install and then use a third-party library. An example of such library is prompt. Some people use these third-party libraries because they help to validate the user input.
var prompt = require('prompt');

  //
  // Start the prompt
  //
  prompt.start();

  //
  // Get two properties from the user: username and email
  //
  prompt.get(['username', 'email'], function (err, result) {
    //
    // Log the results.
    //
    console.log('Command-line input received:');
    console.log('  username: ' + result.username);
    console.log('  email: ' + result.email);
  });

Reference: https://github.com/flatiron/prompt

2
  • I think this works fine. Are there any other options aside from these that you can suggest?
    – codecumber
    Sep 8, 2020 at 11:21
  • For Visual Studio Code You can use the Quokka plugin or the .NET Notebook plugin. Contrary to its name, the .NET Notebook supports other languages such as JS.
    – Steve Cruz
    Mar 1, 2023 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.