How to get user input using prompts in JavaScript

The JavaScript user input

The user input in JavaScript is a method used to request input data. In layman’s terms, it is like a fruit basket that is used to collect fruit from the farm and later process for storage.

Once data is received and “Enter” is pressed, the data is returned. Otherwise, it returns null.

Syntax

prompt(data)

Code

Explanation

Line 1: We prompt the user with a pop-up to enter their name.

Lines 3–6: We print a statement to the screen, welcoming the user by name.