How to Combine Input and Output in JavaScript

By Chris Minnick, Eva Holland

In JavaScript, you can combine input and output to display customized output, based on input from a user. This is really the heart of what JavaScript can do for web pages!

Follow these steps in the JavaScript Console to create a letter to yourself in your web browser. Make sure to press Return or Enter after the end of each statement (after each semicolon).

  1. Type the following to create a variable containing your first name.

    var toName = "your name";
  2. Type the following to create a variable containing the person the letter is from:

    var fromName = "The Grammy Awards";

    You can change The Grammy Awards to anyone you’d like to get a letter from.

  3. Type the contents of your letter into a variable.

    Use <br> to insert line breaks and don’t press Return or Enter until after you type the semicolon.

    Here’s an example of the letter:

    var letterBody = "We are pleased to inform you that your song, 'Can't Stop Coding!,' has been voted the Best Song of All Time by the awarding committee.";
  4. Write document.write() statements to output each of the three parts of your letter.

    For example:

    document.write("Dear " + toName + ",<br><br>");
    document.write(letterBody + "<br><br>");
    document.write("Sincerely,<br>");
    document.write(fromName);

    When your letter is done, it should resemble the one shown here.

    A fully customized letter displayed in the browser.

    A fully customized letter displayed in the browser.