JavaScript For Kids For Dummies
Book image
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 authors:

Chris Minnick and Eva Holland are experienced web developers, tech trainers, and coauthors of Coding with JavaScript For Dummies. Together they founded WatzThis?, a company focused on training and course development.

This article can be found in the category: