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:
expressionis evaluated once and compared with the values in thecaseclauses.casevalues are compared using strict equality (===), meaning that both value and type must match.- The
breakstatement prevents the code from falling through to the next case. If omitted, the next case will be executed regardless of whether it matches. - The
defaultcase is optional and is executed if none of thecasevalues 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:
forLoopwhileLoopdo...whileLoopfor...inLoopfor...ofLoop
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 = 0initializes the loop counter (i). - Condition:
i < 5runs the loop as long asiis less than 5. - Increment:
i++increasesiby 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:
trueif the user clicks OK.falseif 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.
nullif they click Cancel.
Syntax:
end of part 2 -------->
Explore Other Chapters