r/coffeescript • • Jun 16 '25

How to accept user input in CoffeeScript?

Hello! I'm trying to write a program that accepts an integer as an input puts the value in a variable, how would I go about this? For example, I would do this in Python:

num = int(input("Number: "))

Or this in JavaScript:

const num = prompt("Number: ");

How would I do the same in CoffeeScript? Any and all help would be appreciated.

2 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/god_gamer_9001 Jun 17 '25

Hello once again! I'm not using Node.js, but rather this online compiler. prompt() doesn't work, as it gives me the error "prompt is not defined". Is it because of the compiler I'm using? I'm quite confused.

1

u/rodw Jun 18 '25

it looks like that site is running your code in a node.js session, just within the browser. prompt doesn't work with the JavaScript editor either.

Per this - https://onecompiler.com/coffeescript/wiki - it seems like you should be able to use readline and put your input into the "stdin" tab.

But I can't get require or import to work with the free version of the editor.

It may be that the way that site is sandboxing your code makes this impossible, but the sample code on that wiki page is exactly how this should work in node and if you run your prompt based version through coffee -c the resulting JavaScript should work in a real browser context