🖥️
Intro to Computer Science (ICS3U/C)
  • An Introduction to Computer Science
  • Videos & Slides
  • Unit 1: In the Beginning
    • The History of Computers
    • Binary & Logic
      • Bits and Bytes (Binary)
      • Transistors (Changing Bits)
      • Logic Gates
        • Poster
        • Logic.ly
    • The Parts of a Computer
  • Unit 2: Intro to Code
    • How Do We Code?
      • Coding Conventions (Rules)
      • Commenting Code
    • What is HTML?
      • Hello World! (in HTML)
      • HTML Slideshow
    • Hello World!
    • Input / Output
      • The Console
      • Prompt, Alert, Confirm
    • Variables & Data
      • Strings (Text)
      • Numbers (Values)
        • Converting & Rounding
        • The Math Object
          • Random Numbers
      • Booleans
        • Truthiness
      • Arrays
  • Unit 3: Control Flow
    • Conditionals (if this, do that)
      • If...Else
        • Logical Operators
      • Switch / Case
      • Ternary Operators
    • Loops (Repeating Code)
      • For...Loop
      • While & Do/While Loops
    • Debugging
  • Unit 4: Functions
    • Functional Programming
    • User Defined Functions
      • Hoisting and Scope
    • Calling a JS Function
  • TL;DR
    • Programming Basics
    • Slideshows & Demos
    • Javascript Syntax Poster
  • Advanced Topics
    • Recursion
    • Structures & Algorithms
    • Mmm... Pi
  • External Links
    • Typing Club!
    • repl.it
    • Khan Academy
    • Geek Reading
    • ECOO CS Contest
Powered by GitBook
On this page
  • Prompt
  • Alert
  • Confirm
  1. Unit 2: Intro to Code
  2. Input / Output

Prompt, Alert, Confirm

Javascript's Input/Output functions.

PreviousThe ConsoleNextVariables & Data

Last updated 6 years ago

Prompt

Javascript provides a simple way to collect input from the user - the prompt() function:

// Input must be stored in a variable or else it goes nowhere!
let userResponse = prompt("What is your name?");

The command has two arguments. prompt(text, [defaultText]) and returns a string of what the user entered. It is important to note, that it returns a string.

If the user clicks cancel, then the prompt() function returns null.

Alert

The alert() function is very simple. It takes a string and returns nothing. To use it, you just call it with whatever you want shown in the pop-up box.

alert("The sky is falling, the sky is falling!");

Confirm

Maybe you don't want text from the user but rather a YES or NO (or perhaps OK or CANCEL). Javascript provides the confirm() function to ask the user a question and present them with two choices that equate to true or false.

The ability to retrieve a boolean value from the user can be very useful. The confirm() function takes a string as input and returns a boolean.

let userAnswer = confirm("You will become a zombie");

While many examples could be provided of the three methods listed, it is much more rewarding if you try them out yourself.

The is a great way to give output as a developer (testing, errors, warnings) but general users do not see the console window! If you want to give a quick pop-up of information to the user (without any input from the user), you can use the alert() function.

console
prompt
Sign up to continue codingreplit
Logo
General Alert window with OK button
Press OK for yes, CANCEL for no