JavaScript Reference


What is JavaScript?

JavaScript is a scripting language, originally developed by Brendan Eich for Netscape Communications Corp in the early 1990's. It is a multi-paradigm language that interacts with HTML source code, enabling web sites with dynamic content. While client-side web applications are its most common use, JavaScript can also be used in an increasing number of environments.

JavaScript is unrelated to Java, a popular programming language. There are some similarities between the two, but this can be said for most languages. The working name of JavaScript was Mocha, and changed to LiveScript for its first appearance in the Netscape Navigator web browser. The name was changed again, around the time that Netscape began supporting Java, which became a controversial move - it has been implied that Netscape was trying to capitalize off the success of Java.

Getting Started

While other applications exist, we'll focus on using JavaScript to enhance a website. You don't need anything besides a text editor to get started. After creating an HTML document, you can choose to put your JavaScript code within the HTML file, surrounded by script tags:
<script type="text/javascript">
// Your code here!
</script>
or within a separate file. If you create a file with an extension of .js, you can refer to it from your HTML document by providing a path to your file. For instance, if you have a file called "goodcode.js" then somewhere in your HTML file (frequently between the head tags, but sometimes between the body tags) you'll include the file with the line
<script src="path/goodcode.js"></script>
where path is replaced by the location of your file.

The file can also be located on another server entirely, in which case you'll need to provide an url for the file. For an example, this page is using a JavaScript module called Prettify (made by Google) that helps render the code samples on the page. Without it, things would not look nearly as nice. To use the module, the header of this page includes the line
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>

Learn and Practice

Ideally, you're interested in making an actual website hosted on a server (like our department's). In the meantime, you can try JavaScript without worrying about signing in or transferring files. The websites below make it easy to practice and see immediate results:
  • Liveweave
    Work with an HTML file, CSS file, and JavaScript file at once, and see the changes you make in real time. Save and share your files. Also supports several libraries and frameworks we'll look at, like jQuery, Raphael, and D3.
  • repl.it
    Online environment for JavaScript, among others. Does not highlight the interaction between HTML and JS, but helps you practice basic methods like loops and functions.

Useful Links