Learn the Grammar of JavaScript

Learning a programming language is not unlike learning a foreign language. Programming languages all have similar concepts and approaches, such as statements, data types and loops. Each language has its own idiosyncrasies that you need to learn to communicate properly in that particular language.


Spaces, Tabs and Carriage Returns

♦ Required Spaces: it is required to put a blank space between keywords, for example you need a black space after var in the statement.

var lastValue="Code Red";

♦ Tabs, Extra Spaces and Carriage Returns: JavaScripts interpreters ignore extra spaces, tabs and carriage returns. This means that you can place extra space between items or carriage returns to put things on their own lines to make it easier to read the code. On the other hand, you can put a series of statements on a single line if you wish to save space, each statement must end with a semicolon.

In the example below, both statements have the same meaning in JavaScript. The second one is just easier to read for humans.

$("#startBox").click(function(){$("welcomeBox").hide(); $(".greyBack").css("color", "#DDDDDD");});

    $(".greyBack").css("color", "#DDDDDD");

♦ Carriage Returns and Strings: One place you can not have a carriage return is inside of a string. The string value must all be on a single line.

var streetAddress = "2456 Wilson Boulevard" // this IS acceptable

var streetAddress = "2456
Wilson Boulevard"  // this IS NOT acceptable


A JavaScript Statement

Programming is a series of logical statements that need to proceed in a logical order so that the program works properly. A statement is a basic programming unit that ends with a semicolon ;. The semicolon tells JavaScript that you are done with this statement and lets move on to the next one. 

Examples of a statement in JavaScript:

var priorValue = 45;


A JavaScript Command

Commands are statements that instruct the browser to do some action. In JavaScript, commands always include parentheses (). A command can be something simple like a popup alert box or it can be extensive like a multi-line function.

Examples of commands in JavaScript:

alert("Way to go!");
   // several lines of code


Declaring Variables in JavaScript

A variable is something like a person's name or a number. It will hold a piece of information that the code will use when it is run. The value of the variable may change during the running of the code. The variable may have an empty value until the code is processed. To declare a variable, you need to tell JavaScript that it is a variable. The variable does NOT need to start with a "$" sign as in PHP. Here are some examples of variables that you might use with JavaScript.

var firstName;
var sumTotal = 265;

Variable names are called identifiers. There are a few rules about how a variable can be named in JavaScript.

  • the variable name can not be one of the reserved words that JavaScript uses for other functionality
  • the variable name can only have letters, numbers, dollar signs and underscores.
  • the variable name can not have any other characters other than what is listed above
  • the first character of a variable can not be a number

Variables should be given logical names so that they are easy to remember and logical when you go to look at the code in 6 months.

You must declare the variable if you are going to use it later on in the code. If you have a set of variables, you can declare each one on its own line ending with a semi-colon, or you can separate the variables with commas.

var fName = "John",
var lName = "Smith";
var streetAddress = "438 Oak Street";

var fName = "John", lName = "Smith", streetAddress = "438 Oak Street";

var counter; 

You can give the variable a value at the point of declaration, an empty value by using a set of quotes "" or you can choose to not give it a value upon declaration. A variable with no value is known as undefined. In the example above, the variable counter would be undefined.

If the variable is a number, you simply put the number without quotes. If the variable is a string, you need to put it inside quotation marks. A name would be a string.

To find out more about variables, visit the jQ.Zemplate article: JavaScript variables and values


Reserved Words

JavaScript has a list of reserved words that can not be used as identifiers for variables. These are words that currently have some meaning in JavaScript or are reserved for future development.

You can refer to this page at learn.jquery.com/javascript-101/reserved-words for the list of reserved words.


Using comments in JavaScript

There may be times that you wish to leave a comment to explain what is happening in the code for future reference. Comments must be separated from the code or it can stop the running of the code in its tracks.

Single line comments cans start with two forward slashes //.

A block of lines for comments can start with /* then end with */

// this is a single line comment

/* this would be a block of lines
for a lengthy comment */