What is user input?
An important part of computer science is creating interactive programs. Think of an arcade game: it asks for your name before you play, then greets you by that name on the high score screen. That back-and-forth is user input: letting the person running your program type information into it.
In JavaScript on CodeHS, we use the readLine() function to ask the user for text. The message you put inside the parentheses is the prompt: the question shown to the user. The program pauses and waits, and once the user types an answer, that answer is returned.
let answer = readLine("Ask your question here: ");
Notice that we store the answer in a variable. That is the key move: once the answer lives in a variable, the rest of your program can use it - print it, compare it, do math with it, anything. Without a variable, the user's answer would vanish the instant they gave it.
Two jobs in one line: readLine() shows the prompt and collects the answer. The variable on the left of the = is where the answer gets saved.
Outside CodeHS? Plain browser JavaScript has a built-in equivalent: let answer = prompt("Ask your question here: ");. It pops up a small dialog box, and just like readLine(), it hands your program the answer as a string.
Example: read and greet
Take a look at this program to get a feel for how user input works. Press Run and read the output carefully, with these guiding questions in mind:
- What do you notice about the type of value readLine() returns?
- Why is it important to store the user's answer in a variable?
// Gets user input let name = readLine("What is your name? "); let num = readLine("What is your favorite number? "); // Uses user input to print out information println("Hello " + name + "!"); println(num + "?! That's my favorite number too!"); // Prints out the variable type println("name is a " + typeof name); println("num is a " + typeof num);
What is your name? Ada What is your favorite number? 7 Hello Ada! 7?! That's my favorite number too! name is a string num is a string
Look closely at the last line. The user typed the number 7, but typeof says num is a string, not a number. To JavaScript, the answer "7" is text, just like "hello" is text. Hold that thought - it matters a lot in a minute.
The "52" gotcha: string + number
Here is the number one thing to remember from this tutorial: readLine() always returns the answer as a string, even if the user types a number. And unlike some languages, JavaScript will not crash when you do math with it. It does something sneakier: the + sign glues the values together instead of adding them.
let num = readLine("Pick a number: "); println(num + 10);
Pick a number: 5
510
The user picked 5, so we expected 15. But num holds the string "5", so "5" + 10 becomes the string "510". No error message, no crash, just a quietly wrong answer. These bugs are extra tricky to spot!
Same trap in the browser: prompt() also returns a string, so "5" + 2 is "52" there too. The browser fix is parseInt(): let num = parseInt(prompt("Pick a number: ")); converts the answer into a real number (use parseFloat() for decimals).
readInt() and friends
You could wrap every answer in parseInt() yourself... or you could let the CodeHS JavaScript library do it for you. It includes a whole family of input functions, one for each type of answer you expect:
- readLine(prompt) reads a string (text) from the user
- readInt(prompt) reads an integer (whole number) from the user
- readFloat(prompt) reads a float (decimal number) from the user
- readBoolean(prompt) reads true or false from the user
Take a look at this program. It is almost identical to the first example, but line 2 now uses readInt(). Run it, enter the same answers as before, and compare the last two lines of output. What changed, and why?
let name = readLine("What is your name? "); let num = readInt("What is your favorite number? "); println("Hello " + name + "!"); println(num + "?! That's my favorite number too!"); // Prints out the variable type println("name is a " + typeof name); println("num is a " + typeof num);
What is your name? Ada What is your favorite number? 7 Hello Ada! 7?! That's my favorite number too! name is a string num is a number
Now num is a real number, so math works: num + 10 gives 17, not "710". Pick your input function based on the type of answer you expect. Bonus: if the user types something that is not a whole number, readInt() politely asks again.
The arcade input machine
The best way to understand user input is to be the user. The arcade machine below runs the JavaScript on the left, one highlighted line at a time. Every time the program reaches a readLine() or readInt(), the screen stops and waits for you to type a real answer. Press Insert coin to start.
Press "Insert coin" to power up the machine.
Try the Ticket counter tab too. It has the "52" bug hiding in it - watch what happens to your bonus tickets, then press the fix button to see readInt() save the day with your same answer.
Using input in your program's logic
Printing the answer back is just the start. The real power of user input is using it to make decisions. Once an answer is stored in a variable, you can compare it, calculate with it, and steer your program in different directions with an if statement:
let age = readInt("How old are you? "); if (age < 13) { println("Welcome to the kids' arcade zone!"); } else { println("Welcome to the main arcade floor!"); } println("Next year you will be " + (age + 1) + ".");
How old are you? 12
Welcome to the kids' arcade zone!
Next year you will be 13.
Why readInt() here? Both lines depend on age being a real number. With readLine(), the last line would print "Next year you will be 121." for a 12 year old. Choose your input function based on what you plan to do with the answer.
Practice: yearly salary
You are putting together a report on the minimum wage in your state. Write a program that asks the user for the minimum wage of their state, then calculates and prints the yearly salary. Assume a full work week (40 hours) for the entire year (52 weeks). Store the minimum wage and the yearly salary in variables.
Yearly Salary
Your output should look something like this:
What is the minimum wage in your state? 10.25
The average yearly salary for someone who works 40 hours
per week for 52 weeks is $21320
Hint: choose your user input command carefully based on the type of data you are collecting. Can a minimum wage have a decimal point?
// readFloat() because a wage like 10.25 has a decimal let wage = readFloat("What is the minimum wage in your state? "); // 40 hours per week, 52 weeks per year let salary = wage * 40 * 52; println("The average yearly salary for someone who works 40 hours per week for 52 weeks is $" + salary);
What is the minimum wage in your state? 10.25
The average yearly salary for someone who works 40 hours per week for 52 weeks is $21320
readInt() would chop 10.25 down to a whole number, and readLine() would hand you a string that multiplies into nonsense. readFloat() keeps the decimal, so 10.25 * 40 * 52 gives exactly 21320.
Quick quiz
1. No matter what the user types, what type of value does readLine() return?
2. The user types 5. What does this program print?
let num = readLine("Pick a number: "); println(num + 2);
3. Which line correctly asks for a whole number the program can do math with?
Keep learning JavaScript
You can now build programs that talk with their users. Next, get comfortable with the variables that store those answers, or use if/else statements to make your program react differently to what the user says.