Logo

Programming-Idioms

History of Idiom 120 > diff from v34 to v35

Edit summary for version 35 by 1.7.4:
New JS implementation by user [1.7.4]

Version 34

2018-12-17, 19:00:59

Version 35

2019-01-23, 18:53:08

Idiom #120 Read integer from stdin

Read an integer value from the standard input into variable n.

Idiom #120 Read integer from stdin

Read an integer value from the standard input into variable n.

Code
const {createInterface} = require('readline')

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

rl.question('Input an integer: ', response => {
  let n = parseInt (response)
  // stuff to be done with n goes here

  rl.close()
})
Comments bubble
This example only works with nodeJS (server-side JS) because browser JS does not have a standard input.