This article or tutorial is a work in progress and is still being worked on by other users. Give it some time to mature.

JavaScript is the premier client-side scripting language used on the Internet. It can do a plethora of things ranging from the validation of form data to the creation of complex user interfaces. JavaScript is the language for HTML, for the Internet, for computers, servers, laptops, tablets, smartphones, and more.

JavaScript is the interactive member of the triangle of Web technologies, along with HTML and CSS, and can be used to dynamically manipulate the content, markup, and style of Web pages.

Hello JavaScript World

The first JavaScript example we will use is the standard "Hello World" example. We use JavaScript to write the string "Hello JavaScript World" into a simple HTML document:

<!DOCTYPE html>
  <meta charset="UTF-8">
  <title>Hello JavaScript World</title>
<h1>My First JavaScript</h1>
<hr />
  document.write("Hello JavaScript World!");

Take note of how the script is included directly into the markup using the <script> element, which encloses the following one-line script:

document.write("Hello JavaScript World!");

In HTML, you can copy the source code above in <script></script> elements. By doing so, the browser can differentiate between what is HTML and what is JavaScript or normal text.

Hello World Errors

Adding JavaScript to HTML Documents

There are four ways to include script in an HTML document:

  • Within the <script> element
  • Within an HTML event handler attribute such as onclick
  • As a linked file referenced using the src attribute of the <script> element
  • Using the pseudo-URL javascript: syntax within some link

The <script> Element

The primary and most commonly used method for including JavaScript within HTML or XHTML is the <script> element. A script-aware browser assumes that all text within the <script> element is to treated as some form of a scripting language; in the case with the common browsers, this scripting language is JavaScript.

  alert("This is JavaScript");

However, it is possible for the browser to support other scripting languages. For instance, the Internet Explorer family of browsers natively supports VBScript.

The language Attribute

The traditional, albeit nonstandard, way to indicate the scripting language being used is to specify the language attribute for the tag, like so:

<script language="JavaScript">
  alert("This is JavaScript");

However, what follows is an example of how to indicate that VBScript is in use:

<script language="VBS">
  msgBox "This is VBScript"

The type Attribute

However, according to the W3C HTML syntax, the language attribute is not safe. Instead, the type attribute should be set to indicate the MIME type of the language in use. Some examples of JavaScript MIME types include:

  • text/javascript
  • application/javascript
  • application/x-javascript
  • text/ecmascript
  • application/ecmascript

For safety reasons, users are to use solely the text/javascript MIME type, as it is cross-browser compatible:

<script type="text/javascript">
  alert("This is JavaScript");

However, VBScript can also be specified, as is the case here:

<script type="text/vbscript">

Using the <script> Element

Script in the <head>

Event Handlers

Linked Scripts

Linked Script Consideration

JavaScript Pseudo-URL

JavaScript Usage Considerations

Script Masking

The <noscript> Element

Language Versions