Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
JavaScript version of while True loop

I wrote this bit of code

while True:
   text = input('type here > ')
   print text

I've tried the below but it doesnt seem to be working

I'm having bit of a struggle trying to create a JavaScript version of it, since it seems js while loops constantly rerun but python seems to wait for input before rerunning. I am using the readline module to receive input from the console.

const readline = require('readline');

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

while (true) {
 rl.question('type here > ', text => {
       console.log(text)
    })
}

Is there something im getting wrong? Im fairly new to programming

Any solutions?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

If what you are trying to do is get input from the console in JavaScript you can use NodeJS process.stdin the implementation below :

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let input = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    input += inputStdin;
});

process.stdin.on('end', _ => {
    input = input.trim()
        .split('\n')
        .map(str => str.trim());

    // calling the function that works on the input
    doSomething();
});


function readInput() {
    return input[currentLine++];

}

Sample function to do something with what you get from the console

function doSomething() {
    console.log(readInput())
}
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!