Javascript Basics
Printer Friendly Page
Declare variables
[var is lower case]
var cust;
var balance;
Declare on one line.
var cust, balance;
Declare and assign values together.
var cust = "Smith, John";
var balance = 456.30;
var totdue = price * .06;
Relational Operators
- less than<
- less than or equal to<=
- greater than>
- greater than or equal to>=
- equal==
- not equal (!=)
Arithmetic Operators
- ADD +
- SUBTRACT-
- MULTIPY *
- DIVIDE /
- MODULO %
- INCREMENT++
- DECREMENT--
Escape Sequences
- Backspace \b
- Form feed\f
- New line\n
- Carriage return \r
- Single quote \'
- Double quote \"
- Single backlash \\
Assignment Operators
| Operator |
Example |
Process |
Description |
| = |
a=b |
a=b |
Assign |
| += |
a+=b |
a=(a+b) |
Add value then assign |
| -= |
a-=b |
a=(a-b) |
Subtract value then assign |
| *= |
a*=b |
a=(a*b) |
Multiply value then assign |
| /= |
a/=b |
a=(a/b) |
Divide value then assign |
| %= |
a%=b |
a=(a%b) |
Modulus value then assign |
if (expression) do this;
var totalnum = 5;
if (totalnum > 0) alert ("The number is " + totalnum; )
OR
var totalnum = 5;
if (totalnum > 0)
{
alert ("The number is " + totalnum; )
}
if (expression) do this ; else do this ;
var totalnum = 5;
var bool;
if (totalnum > 0)
{
bool == true;
}
else
{
bool == false;
}
if (expression) do this ; else if (expression) do this ;
var totalnum = 5;
var bool;
if (totalnum > 0)
{
bool == true;
}
else if (totalnum < 0)
{
bool == false;
}
else
{
alert('The amount is 0')
}
Javascript in HTML
<script type="text/javascript">