Javascript Blocks
Javascript Blocks
JavaScript statements can be grouped together in blocks. Blocks start with a left curly bracket {, and
ends with a right curly bracket }.
Variables declared outside a function become GLOBAL, and all scripts and
functions on the web page can access it.
If you declare a variable, without using "var", the variable always becomes
GLOBAL.
Comparison Operators
Given that x=5, the table below explains the comparison operators:
Logical Operators
Conditional Operator
JavaScript also contains a conditional operator that assigns a value to a variable based on some condition.
Syntax
variablename=(condition)?value1:value2
Example
If condition syntax
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and condition2 are not true
}
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
-Alert Box
An alert box is often used if you want to make sure information comes through to
the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
alert("sometext");
-Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to
proceed.If the user clicks "OK", the box returns true. If the user clicks "Cancel",
the box returns false.
Syntax
Var x = confirm("sometext");
-Prompt Box
A prompt box is often used if you want the user to input a value before entering a
page. When a prompt box pops up, the user will have to click either "OK" or
"Cancel" to proceed after entering an input value. If the user clicks "OK" the box
returns the input value. If the user clicks "Cancel" the box returns null.
Syntax
Var x = prompt("sometext","defaultvalue");
The return statement is used to specify the value that is returned from the
function.
So, functions that are going to return a value must use the return
statement.
The example below returns the product of two numbers (a and b):
function product(a,b)
{
return a*b;
}
for (i=0;i<=5;i++)
{
//Code to be executed
}
While syntax
while (variable<=endvalue)
{
code to be executed
}
Do while syntax
do
{
code to be executed
}
while (variable<=endvalue);
The break statement will break the loop and continue executing the code
that follows after the loop
The continue statement will break the current loop and continue with the
next value.
Example
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
document.write(person[x] + " ");
}
Events are normally used in combination with functions, and the function will not be
executed before the event occurs!For a complete reference of the events recognized
by JavaScript, go to our complete JavaScript reference.
The try...catch Statement
try
{
//Run some code here
}
catch(err)
{
//Handle errors here
}
The table below lists other special characters that can be added to a text
string with the backslash sign
Code Outputs
\' single quote
\" double quote
\\ backslash
\n new line
\r carriage return
\t tab
\b backspace
\f form feed
Create an Array
An array can be defined in three ways. The following code creates an
Array object called myCars:
1:
var myCars=new Array(); // regular array (add an optional integer
myCars[0]="Saab"; // argument to control array's size)
myCars[1]="Volvo";
myCars[2]="BMW";
2:
var myCars=new Array("Saab","Volvo","BMW"); // condensed array
3:
var myCars=["Saab","Volvo","BMW"]; // literal array
The Boolean object has no initial value, or if the passed value is one of the
following:
0, -0, null, "", false, undefined, NaN. The object it is set to false. For any other
value it is set to true (even with the string "false")!