📚 Overview
In CodeHS, you use the readLine() function to get user input. This is different from standard JavaScript which uses prompt(). CodeHS provides a custom library that makes input handling easier for beginners.
💡 Key Point: CodeHS uses
readLine() as a simplified way to get user input. This function is specific to the CodeHS platform and won't work in regular JavaScript environments.
🔧 Main Input Methods
readLine(prompt)
Purpose: Reads a line of text from the user
Returns: String
let name = readLine("What is your name? ");
println("Hello, " + name + "!");
Try it: readLine() Demo
Your output will appear here...
readInt(prompt)
Purpose: Reads an integer from the user
Returns: Integer (number)
let age = readInt("How old are you? ");
println("In 10 years, you will be " + (age + 10));
Try it: readInt() Demo
Your output will appear here...
readFloat(prompt)
Purpose: Reads a decimal number from the user
Returns: Float (decimal number)
let price = readFloat("Enter the price: $");
let tax = price * 0.08;
println("Total with tax: $" + (price + tax));
Try it: readFloat() Demo
Your output will appear here...
readBoolean(prompt)
Purpose: Reads a true/false value from the user
Returns: Boolean (true or false)
let hasLicense = readBoolean("Do you have a driver's license? ");
if (hasLicense) {
println("You can drive!");
} else {
println("You need to get your license first.");
}
Try it: readBoolean() Demo
Click True or False...
🎯 Complete Example
Here's a complete program that uses multiple input methods:
// Get user information
let name = readLine("What is your name? ");
let age = readInt("How old are you? ");
let height = readFloat("How tall are you in meters? ");
let likesJS = readBoolean("Do you like JavaScript? ");
// Display results
println("===== YOUR PROFILE =====");
println("Name: " + name);
println("Age: " + age);
println("Height: " + height + "m");
println("Likes JavaScript: " + likesJS);
🎮 Interactive Complete Example
Fill in all fields and click Yes or No...
⚠️ Common Mistakes & Tips
1. Not storing the input in a variable
// ❌ Wrong - input is not stored
readLine("What is your name? ");
println(name); // Error: name is not defined
// ✅ Correct - store in a variable
let name = readLine("What is your name? ");
println(name);
2. Using readInt() for decimal numbers
// ❌ Wrong - readInt() truncates decimals
let price = readInt("Enter price: "); // 19.99 becomes 19
// ✅ Correct - use readFloat() for decimals
let price = readFloat("Enter price: ");
3. Forgetting to convert readLine() output
// ❌ Wrong - readLine() returns a string
let num = readLine("Enter a number: ");
println(num + 5); // If input is "10", this prints "105"
// ✅ Correct - use readInt() or convert
let num = readInt("Enter a number: ");
println(num + 5); // This prints 15
💡 Pro Tip: Always use the most specific input function for your data type. This prevents type conversion errors and makes your code clearer!
📋 Quick Reference
| Function | Input Type | Example |
|---|---|---|
readLine() |
Text/String | "Hello World" |
readInt() |
Whole Number | 42, -7, 100 |
readFloat() |
Decimal Number | 3.14, 19.99, -2.5 |
readBoolean() |
True/False | true, false |