Maharashtra's Most Trusted
Chapter 1 - Basics of Java Script Programming
Part 2
Conditional Statement
In JavaScript, a conditional statement is used to perform different actions based on different conditions. These conditions are evaluated as either true
or false
, and the flow of the code changes based on the result of these evaluations. The most commonly used conditional statements in JavaScript are:
1. if
Statement
The if
statement executes a block of code if a specified condition is true
.
2. if-else
Statement
The if-else
statement extends the if
statement by providing an alternative block of code that is executed if the condition is false
.
3. else if
Statement
The else if
statement is used to specify a new condition to test if the first condition is false
.
Switch statement in Java Script -:
In JavaScript, the switch
statement is used to execute one block of code among many options, based on the value of an expression. It’s a more organized and readable way to handle multiple if-else
statements when you are comparing a variable against many different values.
Syntax of switch
statement:
Key Points:
expression
is evaluated once and compared with the values in thecase
clauses.case
values are compared using strict equality (===
), meaning that both value and type must match.- The
break
statement prevents the code from falling through to the next case. If omitted, the next case will be executed regardless of whether it matches. - The
default
case is optional and is executed if none of thecase
values match the expression.
Example 1: Basic switch
statement
Example 2: default
case and fall-through behavior
Looping statement in Java Script -:
In JavaScript, loops are used to repeatedly execute a block of code as long as a specified condition is true
. They are useful when you need to perform repetitive tasks, iterate over arrays or objects, and avoid writing redundant code.
Types of Loops in JavaScript:
for
Loopwhile
Loopdo...while
Loopfor...in
Loopfor...of
Loop
1. for
Loop
The for
loop is used when you know in advance how many times you want to execute a statement or block of code.
Syntax:
- Initialization:
let i = 0
initializes the loop counter (i
). - Condition:
i < 5
runs the loop as long asi
is less than 5. - Increment:
i++
increasesi
by 1 after every iteration.
Output:
2. while
Loop
The while
loop repeats the block of code as long as the specified condition is true
. It checks the condition before executing the loop body.
Syntax:
3. do...while
Loop
The do...while
loop is similar to the while
loop, but it executes the block of code at least once, even if the condition is false
initially. The condition is checked after executing the loop body.
Syntax:
Loop Control Statements:
break
: Terminates the loop entirely.continue
: Skips the current iteration and moves to the next one.
Example using break
:
Output:
Example using continue
:
Alert box , Confirm Box & Prompt Box
In JavaScript, the alert()
, confirm()
, and prompt()
boxes are used to interact with the user. Each of these methods displays a pop-up dialog box that pauses the script execution until the user takes action.
1. alert()
Box
The alert()
method displays a simple message in a dialog box with an OK button. It is often used to show informational messages to the user. The user can only dismiss it by clicking the OK button.
Syntax:
2. confirm()
Box
The confirm()
method displays a dialog box with a specified message, along with two buttons: OK and Cancel. It is used to ask the user for confirmation (yes/no or true/false decisions). The method returns a boolean value:
true
if the user clicks OK.false
if the user clicks Cancel.
Syntax:
3. prompt()
Box
The prompt()
method displays a dialog box that asks the user for input. The box includes a text field where the user can enter a value. It also has OK and Cancel buttons. The method returns:
- The user’s input (a string) if they click OK.
null
if they click Cancel.
Syntax:
end of part 2 -------->
Explore Other Chapters