Home

How to Combine Input and Output in JavaScript

|
Updated:  
2016-03-26 07:26:17
|
JavaScript Essentials For Dummies
Explore Book
Buy On Amazon

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
    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.

About This Article

This article is from the book: 

About the book author:

Chris Minnick is an accomplished author, teacher, and programmer. Minnick authored or co-authored over 20 books, including titles in the For Dummies series. He has developed video courses for top online training platforms and he teaches programming and machine learning to professional developers at some of the largest global companies.

Eva Holland is an experienced web developer, tech trainer, and coauthor of Coding with JavaScript For Dummies. She is a co-founder of WatzThis?, a company focused on training and course development.