0% found this document useful (0 votes)
46 views

Javascript Blocks

JavaScript is a case-sensitive, scripting language. It uses semicolons to separate statements optionally and blocks of code are wrapped in curly brackets. Variables declared globally without the var keyword become global. Comparison and logical operators compare and check relationships between variables. Conditional statements like if/else and switch/case control program flow. Functions can return values using the return statement. Common loops include for, while, and do-while loops. Arrays can be created using array literals or the Array object.

Uploaded by

Jemin Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Javascript Blocks

JavaScript is a case-sensitive, scripting language. It uses semicolons to separate statements optionally and blocks of code are wrapped in curly brackets. Variables declared globally without the var keyword become global. Comparison and logical operators compare and check relationships between variables. Conditional statements like if/else and switch/case control program flow. Functions can return values using the return statement. Common loops include for, while, and do-while loops. Arrays can be created using array literals or the Array object.

Uploaded by

Jemin Joseph
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

JavaScript

 JavaScript is Case Sensitive

 It is normal to add a semicolon at the end of each executable statement. Most


people think this is a good programming practice. The semicolon is optional
(according to the JavaScript standard), and the browser is supposed to interpret
the end of the line as the end of the statement. Using semicolons makes it
possible to write multiple statements on one line.

 JavaScript Blocks [here]

JavaScript statements can be grouped together in blocks. Blocks start with a left curly bracket {, and
ends with a right curly bracket }.

The purpose of a block is to make the sequence of statements execute together.

 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:

Operator Description Example


== is equal to x==8 is false
=== is exactly equal to (value and type) x===5 is true
x==="5" is false
!= is not equal x!=8 is true
> is greater than x>8 is false
< is less than x<8 is true
>= is greater than or equal to x>=8 is false
<= is less than or equal to x<=8 is true

 Logical Operators

Operator Description Example


&& and (x < 10 && y > 1) is true
|| or (x==5 || y==5) is false
! not !(x==y) is true

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

greeting=(visitor=="PRES")?"Dear President ":"Dear ";

 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 case syntax

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
}

 JavaScript Popup Boxes

-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

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 loop syntax

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.

 The for...in statement loops through the properties of an object.

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

 Creating a date object

var mydate = new date();

 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")!

You might also like