0
0
Javascriptprogramming~5 mins

Basic input concepts (prompt overview) in Javascript

Choose your learning style9 modes available
Introduction

We use input to let the user tell the program what they want. It makes programs interactive and personal.

When you want to ask the user their name to greet them.
When you need the user to enter a number for a calculation.
When you want to get feedback or choices from the user.
When you want to customize what the program does based on user answers.
Syntax
Javascript
let variableName = prompt('Your question here');

The prompt function shows a box asking the user to type something.

The text inside the quotes is the question or message shown to the user.

Examples
This asks the user for their name and saves it in the variable name.
Javascript
let name = prompt('What is your name?');
This asks the user for their age and saves it as a string in age.
Javascript
let age = prompt('How old are you?');
This asks the user for their favorite color and stores it in color.
Javascript
let color = prompt('What is your favorite color?');
Sample Program

This program asks the user for their name and then shows a greeting message using an alert box.

Javascript
let userName = prompt('Please enter your name:');
alert('Hello, ' + userName + '! Welcome!');
OutputSuccess
Important Notes

The prompt function always returns a string, even if the user types numbers.

If the user clicks cancel, prompt returns null.

Use alert to show messages to the user after input.

Summary

Use prompt to ask the user for input in a simple box.

The input is saved as a string in a variable for your program to use.

Input makes your program interactive and personal.