Skip to content

JavaScript basics

lmccart edited this page Oct 21, 2014 · 109 revisions

JavaScript is a scripting language that is typically used on web pages where it runs client-side (within the web browser). It is a general purpose language with a lot of built-in functionality for interacting with elements on a webpage and responding to actions initiated by the user.

Although the JavaScript has "Java" in it's name, it isn't related other than by the fact that it looks something like Java. JavaScript's official name is ECMAScript (ECMA is the European Computer Manufacturers Association, a standards body). It was initially created by Netscape Communications. (Uncyclopedia: JavaScript)

##The <script> tag

JavaScript can be placed anywhere within an HTML document, although it is typically included in the "head" section of the HTML, and is specified by the use of <script> tags:

<html>              
  <head>              
    <script type="text/javascript">              
      //JavaScript goes here

    </script>
  </head>              
</html>              

You can also write JavaScript in file external to the HTML and point to that file in a script tag.

<script type="text/javascript" src="myscript.js"></script>

##Comments

Comments in JavaScript are similar to comments in Java or C:

  // single line comment   
  /* 
    multiple
    line
    comment
  */

Console

One of the first things we probably want to learn is how to get debugging output. You can write to the console by using the built-in console.log method:

console.log("hello");

In order to see the console on Chrome, select "View" > "Developer" > "JavaScript Console". Use it often!

Variables

Operators

Logic

Arrays

Functions

Variable scope

Clone this wiki locally