Javascript Syntax Need to Know

README Now

As someone that has struggled to understand Javascript for well over ten years I can tell you that knowing the information presented here will save you a lot of time and heartache. I program/have programmed professionally in C, VB, Java, Cobol, Perl, and Php. Each language presents its own set of quirks and idoims you need to learn about. Many one must discover through the school of experience. But, there are some things it is just better to be told.

The Audience I am Writing To

I am writing this for my fellow programmer. I expect that you have ample experience as a programmer. In at least one block language. All of the languages I list about are block languages except for Cobol and VB. I also expect that you have programmed in at least one language that allows you to write functions (or methods as in Java) and do argument passing. This rules out Cobol. If all you know is Cobol this writeup may not be adequate. It is likely that you will need to understand more about variable scope and building and using functions.

I am just starting to get my act together regarding this language. It is my intent to do some serious engineering with this language and I am acutely aware that in order to do this I must need a better understanding of this language. The Good, Bad, and Ugly.

 

The Current List (thus far)

 

Variable Scope

Though Javascript employs blocks in its syntax it DOES NOT employ block scoping of variables. variables declared and used within a function are accessible by any part of the function. If you are writing functions that contain lots of different blocks this is really an important point to remember. [back]

 

Stop using Global variables

Global variables are pretty much any variable not declared within a function. But, if you do not declare a variable using the var verb it will be considered a global variable. So, always declare your variables in Javascript before using them.

var myvar;	

 [back]

 

Where you place the { MATTERS

When I found this one out I almost when ballistic. Without going into the ugly details it is important that you DO NOT place { on its own line. This has always been my coding style. Not with Javascript!

DO

function foo(a) {

}	

DON'T

function foo(a) 
{

}		

Follow this for any block structure [back]

 

use blocks

Javascript will let you get away in some cases without using blocks. You need to use blocks all the time where they are required [functions, conditionals, loops]. [back]

 

use Semi-colon

Java script is sloppy and allows you to forget the ;. It will add them in for you. Unfortunately it can add them in the wrong places. Given that Javascript does not have great error checking at either development time or at runtime you do not want to deal with this type of situation. The ; belongs at the end of any statement. They do not go after the end block }! [back]

 

Use JQuery

This is not an issue with with Javascript. This is an issue with the DOM. JQuery is a terrific library for accessing html entities. It is also great for use with CSS. [back]

 

Code formatting

If you have not read what I have written above regarding coding conventions go back above and do so. [back]

 

Which equivalency operator to use

Because Javascript inherently does type coercion before comparing you want to use the === (equal) and !== (not equal) operators. These operators do not do type coercion.  [back]

Type coercion exists in other languages as well. In Javascript the following statement is true.

	var a == 1;
	if (a == true) { ...

Coercion can be deceptive. What you are really testing is the state of a variable. There are certainly cases when you might want to do this. When you need to do this use the following convention instead.

	var a == 1;
	if (a) { ...

If you use these techniques you will not incur the subtle wrath that coercion can place in your path.


If you find this site useful and are a book buyer/reader...

Abe Books is one of my affiliates. I only use affiliates that I also purchase products from. I love Abe books. I have purchased many books from them originally cost $40 to $60 for as little as $5 to $10 dollars including shipping costs.

By accessing Abe books and clicking on one of my links I receive a small commission on your purchase that helps pay for this site. You pay no more for your books than if you accessed their site directly. Thanks for your support.

I have found Javascript - The Good Parts to be an indespensible resource

Click on the book to find books on Javascript at Abe Books...

If you would like a new version of the book click below to buy it from Amazon (also one of my affiliates).

Javascript Examples

Basics

Forms

Misc













navTango.com free

75% of your donation

goes to charity.