spacer
Web Development Learn JavaScript
 Developer Newsletter

Learn JavaScript
JavaScript Tutorial
Introduction to JavaScript
JavaScript Getting Started
JavaScript Syntax
JavaScript Variables
JavaScript Functions
JavaScript If Statement
JavaScript While Loop
JavaScript For Loop
JavaScript Comments
JavaScript Syntax Reference
Advanced JavaScript
JavaScript Switch Statement
JavaScript Arrays
JavaScript Objects
JavaScript getElementById
JavaScript Popup Windows
JavaScript Redirect
JavaScript Cookies
JavaScript Events
JavaScript No Right Click
JavaScript Strings
Detecting Browsers

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

JavaScript Syntax

Let's examine JavaScript's syntax. The syntax of any programming language refers to how the code instructions are written, and what certain symbols mean.

Variables and data types

First, we need to take a look at a little more code.

<script>
text = 'Hello World!';
document.write(text);
</script>

This is another simple example of JavaScript. Here, we are using JavaScript variables. A variable can store information, and has a value. The value can change - which is why we call it a variable. When we want to refer to the value of that variable, we do not need to know the value of the variable, just the name of the variable, which makes it invaluable for development.

Here, we take the variable text and give it a value of 'Hello World!'. We do this with the = equal symbol. A single equal symbol is called the assignment operator. We are assigning the value 'Hello World!' to the variable text. Instead of telling document.write to output a specific text, we tell it to output the value of our variable text.

Notice there are no quotes around our variable name text - variables can hold many values, including numbers and text. A piece of text in JavaScript is called a string. As the contents of a string could be the same as a piece of JavaScript, we put quotes around the string, so that JavaScript knows that it is a piece of text that it does not have to interpret. This is called quoting a string, and any text that is not JavaScript code should be quoted. Either a single quote - ' - or a double quote - " - can be used.

Finally, there is a semicolon at the end of each line. This tells JavaScript where the end of an instruction is. Programming is about writing a series of instructions, and the semicolon shows where one instruction ends and another starts. All instructions should be on a seperate line, and we refer to each line as a statement, or, more commonly, a line of code.

Arithmetic operators

We can add, subtract, multiply and divide numbers, and assign the result to variables, like so:

total = 0;
total = total + 10;
total = total * 2;
total = total / 2;
total = total - 5;

Here, we are operating on the variable total. We start by making it 0, then we add 10 to it, double it, halve it, and finally subtract 5 from it.

Comparison operators

We will cover later the use of conditions and comparison operators. These comparison operators are used to compare two or more values, typically a variable and an absolute value. These are JavaScript's comparison operators:

== equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
!= not equal to

So JavaScript syntax is really quite easy; keep practising and you can master it very quickly. Here's a summary of JavaScript syntax we've learned so far:

variablename = 'New Value';

functionname(variablename);
othervar = 10 + 20;

functionname('Some Text');

Now, let's take a closer look at variables.

« JavaScript Getting Started JavaScript Variables »

More JavaScript Tutorials:
» Pre-Fill Forms From Last Use
» How to Setup a Randomising Function
» JavaScript Debugging Techniques with Firebug
» A Simple Image/Link Rollover in Javascript
» JavaScript Double Click Trapper
» JavaScript Tutorial Part II - Function Basics


About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: