Important Notice:

Prompt box

Prompt box

18 views 2 min read

prompt() – “यूजर से इनपुट लेने वाला” बॉक्स

prompt() बॉक्स का उपयोग यूजर से कुछ टाइप करने (input) के लिए किया जाता है। यह भी pop‑up window में खुलता है।

  • इसमें एक टेक्स्ट फील्ड (लिखने की जगह) होती है + OK और Cancel बटन
  • यह एक वैल्यू वापस (return) करता है:
    • OK दबाने पर यूजरनेजोटाइपकिया (string / पाठ)
    • Cancel दबाने पर  null
  • आप चाहें तो दूसरा पैरामीटर लगाकर पहले से लिखी हुई डिफॉल्ट वैल्यू दे सकते हैं।

prompt() के सामान्य उपयोग (Common Uses of prompt()):

  1. यूजर से नाम पूछना
    जैसे – “कृपया अपना नाम लिखें:”
  2. यूजर से उम्र पूछना
    जैसे – “आपकी उम्र क्या है?”
  3. कोई गणना करने के लिए नंबर लेना
    जैसे – “पहला नंबर दीजिए:”
  4. यूजर से शहर या पता लेना
    जैसे – “आप किस शहर में रहते हैं?”
  5. डिफॉल्ट वैल्यू के साथ उदाहरण
    जैसे – prompt("ईमेल दें:", "user@example.com")

Example:-

<!DOCTYPE html>
<html>
<body>

<script>
  let name = prompt("Enter Your Name");

  document.write("Hello " + name);
</script>

</body>
</html>

prompt() – The "Take User Input" Box

The prompt() box is used to let the user type something (input). It also opens in a pop‑up window.

  • It has a text field (place to type) + OK and Cancel buttons
  • It returns a value:
    • Click OK → whatever the user typed (as a string)
    • Click Cancel → returns null
  • You can also provide a second parameter to set a default value that already appears in the text field.

Common Uses of prompt():

  1. Ask the user for their name
    Example: "Please enter your name:"
  2. Ask the user for their age
    Example: "What is your age?"
  3. Take a number for a calculation
    Example: "Enter the first number:"
  4. Ask the user for their city or address
    Example: "Which city do you live in?"
  5. Example with a default value
    Example: prompt("Enter email:", "user@example.com")
  6. Example:-
<!DOCTYPE html>
<html>
<body>

<script>
  let name = prompt("Enter Your Name");

  document.write("Hello " + name);
</script>

</body>
</html>

Related Notes