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: » Secret Access » Automatically Inserting the Current URL Into Forms » Sending Cookies By Email » Pre-Fill Forms From Last Use » Working... Popup for Forms » JavaScript Double Click Trapper |

