Page 97 - DCAP202_Fundamentals of Web Programming
P. 97
Fundamentals of Web Programming
Notes Discuss about the user-defined function
Describe the use of dialog boxes in JavaScript
Introduction
JavaScript statements consist of keywords used with the appropriate syntax. A single statement
may span multiple lines. Multiple statements may occur on a single line if each statement is
separated by a semicolon. These include flow control (if-else, switch), loops (while, do-while,
for), and loop control (break, and continue). JavaScript also supports some object-related
statements (with, for-in).
Functions are blocks of JavaScript code that perform a specific task and often return a value. A
JavaScript function may take zero or more parameters. Parameters are a standard technique via
which control data can be passed to a function.
JavaScript provides the ability to pick up user input or display small amount of the text to the
user by using dialog boxes. These dialog appear as separate windows and their content depends
on the information provided by the user.
8.1 Conditional Statements
Conditional Statements give the JavaScript code you are writing the ability to make decisions or
perform single or multiple tasks. These Conditional statements were borrowed from older,
more polished languages like C, C++, and Java.
8.1.1 If...Else Statement
The if conditional is used to perform an action “if” the condition is met.
Examine the below syntax example to get a feel for how to build a simple if conditional.
if (expression)
statement;
The example shows the if keyword followed by an expression within brackets. Within these
brackets are the conditions that must be met in order to execute the statement.
If the condition is never met, the statement is never executed. Simply put, the expression must
evaluate to true in order for the statement to be executed. Within this simple conditional are an
infinite number of variations. Basically, you’re letting the script make decisions based on the
condition, the expression, you supply.
While performing an action if a value of true is returned from the expression is great, you will
eventually have a need for something to happen if the expression evaluates to false. Assigning
an action to the false output of the expression involves a very simple addition to the if statement
given above. The else keyword is used to supply an action to be taken if the expression is false.
Examine the syntax example below.
if (expression) {
statement if expression is true;
} else {
statement If expression is False;
Notice the addition not only of the else keyword and an additional statement, but also the use of
the opening and closing curly braces - { } . The braces are used to encapsulate the true statement
and the false statement, as well as divide the entire statement into its “if” and “else” sections.
90 LOVELY PROFESSIONAL UNIVERSITY