Skip to content
Advertisement

How to create user input in the JavaScript console?

I’m creating a short game in the JavaScript console, and for inspiration, I looked at google’s easter egg version (if you search text adventure then press f12 you can see it). I wanted input, the only way I could figure out how to have input was by having functions:

console.log('Do you like chocolate ice cream?');

function yes() {
    console.log('I don't like chocolate ice cream.');
}

function no() {
    console.log('Yeah, I agree. Who even likes chocolate ice cream anyways?');
}

But in google’s version, you can just type yes, and it returns a bunch of text, no parentheses. I tried with variables but it didn’t really look nice and it turned out green. Does anybody know how to make it google’s way?

Advertisement

Answer

You can’t really do that. The closest you can get is

var input = prompt("What is your name?");
console.log("Hello, " + input + "!");
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement