JavaScript Tutorials

Overview

JavaScript can be used to change data on the page, rewrite the entire page, create popup alerts, write to the console for testing, or print the page.

Writing Content to the Page

The Element innerHTML property can be used to set the HTML content of an element. The HTML element is selected by its id #my_output, and the innerHTML (text inside the paragraph element) is changed to “New content”.

<!-- output to the HTML element -->
<p id="my_output">My content</p>
document.getElementById('my_output').innerHTML = 'New content';

Re-Writing the Page

The document.write() method writes directly to an open HTML document. The document.write() method deletes all existing HTML when used on a loaded document.

document.write('Hello World!');

Creating an Alert Popup in the Browser

The Window alert() method displays an alert popup in the browser with a message and OK button, and is used to display information to the user.

alert('Hello World');

Writing to the Browser Console for Testing

The console.log() method logs a message to the browser console, which is useful for testing purposes.

console.log('Hello world');

Printing the Page

The window.print() method opens a print dialog box, which lets the user select from printing options to print the contents of the window.

window.print();

JavaScript Notes:

  • When using JavaScript, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
  • JavaScript is a case-sensitive language; firstName is NOT the same as firstname
  • Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
  • JavaScript variables must begin with a letter, $, or _
  • JavaScript variables are case sensitive (x is not the same as X)

We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint, borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.