Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to take input in JavaScript

Getting Started

Imagine you're at a restaurant. You'd like to order some food, but how would you communicate that to the kitchen? You'd need a waiter, right? In JavaScript, taking input is like ordering food at a restaurant. The user (you, wanting food) provides some data (the order), the JavaScript code (the waiter) takes this data and processes it (delivers the order to the kitchen).

This blog will guide you on how to be that efficient waiter in JavaScript, taking orders, or rather, inputs from the user and processing them.

Understanding User Inputs

In JavaScript, a user input is data that a user provides to a program. It could be anything from a name entered into a form on a website, to a number chosen in a game. When you're writing JavaScript programs, you will frequently need to handle user inputs.

Taking Inputs in JavaScript

There are various ways to take inputs in JavaScript. We'll start with the simplest one - using prompts.

Using Prompts

A prompt is a simple way to ask the user for input. It displays a dialog box that prompts the visitor for input. Here's how to use it:

let userInput = prompt("Please enter your name");
console.log(userInput);

This code will display a dialog box asking the user to enter their name. Once the user enters their name and hits OK, their input is stored in the userInput variable.

From HTML Forms

Another common way to get input in JavaScript is from HTML forms. When you have an HTML form, you can use JavaScript to get the data entered by the user.

Let's say you have a form like this:

<form id="myForm">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname"><br>
  <input type="submit" value="Submit">
</form>

You can get the data entered in the "fname" field using JavaScript like this:

let form = document.getElementById("myForm");
form.onsubmit = function(e){
  e.preventDefault();
  let userInput = document.getElementById("fname").value;
  console.log(userInput);
}

Processing Inputs

Once you've received the user's input, you'd want to process it. Going back to our restaurant analogy, this would be the part where the waiter delivers the order to the kitchen.

Processing input simply means doing something with the data you've received. What you do depends entirely on what your program is meant to do. It could be something simple like displaying the input back to the user, or something complex like using the input in a calculation.

Here's an example of processing an input by displaying it back to the user:

let userInput = prompt("Please enter your name");
alert("Hello, " + userInput);

In this example, the alert function is used to display a dialog box to the user. The text in the dialog box is "Hello, " followed by whatever the user entered as their name.

The Importance of Validating Inputs

Before you process user inputs, it's important to check that the inputs are valid. Validation is like the waiter checking that they got the order right before taking it to the kitchen. Invalid input can cause errors or unexpected results in your program.

Here's how you can validate an input to make sure it's not empty:

let userInput = prompt("Please enter your name");
if(userInput != "") {
  alert("Hello, " + userInput);
} else {
  alert("You didn't enter your name!");
}

Conclusion

Taking in user input is akin to the ebb and flow of a conversation. The user provides the input (starts the conversation), JavaScript takes the input and processes it (responds), and then also validates the input (ensures the conversation stays on track).

Mastering the flow of this conversation is key to creating interactive and dynamic websites. So, like a waiter at a restaurant, always be ready to take the order, ensure it's accurate, and deliver it to the kitchen. Remember, happy customers (or users, in our case) make for a successful business (or website)!

Happy coding, and remember, practice is the key to mastering JavaScript, or any other programming language for that matter. So keep practicing, keep experimenting, and keep learning!