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.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources