javascript syntax

references

          Javascript.com

comments in code

Comments are not shown to the user but they are very useful for the programmer who must maintain or update the code you wrote. Use comments whenever the purpose of the code is not immediately obvious. If you use complex structures or programming tricks, explain them!

For a single comment line we use:
// double slashes - This is a comment line which is not displayed to the user

Over several lines, use a block structure:
/* slash-asterisk to start the block
of comments
and end the block with asterisk-slash like this */






variables in javascript

Variables in Javascript, as in other languages, are symbolic addresses used to store values in the computer's memory.

Caracteristics :
  • Name of variable must begin with a letter or an underscore ( _)
  • Names are case-sensitive - name is not the same variable as Name
  • Declaration may be explicit using var:
                var customername;
  • Declaration may include initialization:
                var customername="Mike";
  • Declaration may be implicit, without using var:
                customername="Mike";
  • The = operator assigns a value to a variable:
                y=5;      y takes on value 5
                city="Chicago";
                var datej=new Date();


the operators

We use the common arithmetic operators, basically the same as Java:

Given that variable y contains 5



Assignment operators assign a value to a variable:

Given that variable x contains 10 and variable y contains 5



With character strings, the + operator is used to do a concatenation:
            name=firstname+" "+lastname;


Relational operators compare variables and values:

Given that variable x contains 5:



Conditional operators are used for multiple compares (and/or):

Given that variable x contains 6 and variable y contains 3:



decision structure

In programming, decision structures are used all the time to control the execution cycle.

if (condition)
      {
      instructions run if condition is true;
      }
else
      {
      instructions run if condition is false;
      }






creating a function

A function is a script written in the <head> section.

The function is activated when the page opens. The function can then be called by an event in the <body> of the document.

In this example, the function will execute an alert (a small message window) when the page opens (event is onload).

<head>
<script type="text/javascript" >
      function myfunction(text)
      {
      alert(text);
      }
</script>
</head>

<body onload="myfunction('Welcome!')">




Top

Previous            Next