If...else Statement:
It evaluates the content whether the conditi
if-else statement is given below.
Syntax:
n is true or false. The syntax of JavaScript
ifexpression)
{
Heontent to be evaluated if condition is true
}
else
{
JIcontent to be evaluated if condition is false
Flowchart of JavaScript If statement:
False
Body of if Body of else
Example:
Example of i
Ise Statement
12Basics of JavaScript Programming
Output:
ais even number
.else if statement:
It evaluates the content only if the expression is true from several expressions. The syntax
of JavaScript if else if statement is given below.
iffexpression!)
t
/icontent to be evaluated if expression] is true
}
else if(expre:
{
/Icontent to be evaluated if expression? is true
}
else iffexpression3)
ion2)
{
Hcontent to be evaluated if expression3 is true
H
else
{
Jicontent to be evaluated if no expression is true
}
Example:
Example of if..clse if Statement
13Basies of JavaScript Programming
Output:
@ Example of for oop
€ > @ O File| CyZambareds/css%.. @ x Ol A ¢
1 to 5 numbers printing using for loop:
JavaScript while loop:
1) The JavaScript while loop iterates the elements for an infinite number of times.
7Basies of JavaScript Programming
2) It should be used if the number of iterations is not known,
3) The syntax of the while loop is given below.
Synta:
while (condition)
{
code to be executed
Exampl
Example of while loop
24Basics of JavaScript Programming
Output:
€ > |S O File | Cyzambaredeycss%20Pr.. 1 wr Ch A® :
Car color:blue Car Make: Toyota
Car color:red Car Make: Audi
Q. Explain prompt() and confirm() method of Java seript with syntax and example.
Answer:
prompt():
1) The prompt () method displays a dialog box that prompts the visitor for input
2) The prompt () method returns the input value if the user clicks "OK". If the user clicks "cancel"
the method returns null.
Syntay
window.prompt (text, defaultText)
Example:
hum
confirm():
1) It displays the confirm dialog box. It has message with ok and cancel buttons.
2) Returns Boolean indicating which button was pressed
Synta
low.confirm("'sometext");
Example :