Internet Technology

Unit Five: XML, DTD, XSTL, XHTML

place your ad here

JavaScript Conditional Statements || OnlineNotesNepal

JavaScript Conditional Statements || OnlineNotesNepal

JavaScript Conditional Statements

Like many other programming languages, JavaScript also allows you to write code that perform different actions based on the results of a logical or comparative test conditions at run time these are called the conditional statements of JavaScript. This means, you can create test conditions in the form of expressions that evaluates to either true or false and based on these results you can perform certain actions.

There are several conditional statements in JavaScript that you can use to make decisions:

  • The if statement
  • The if…else statement
  • The if…else if….else statement
  • The switch…case statement

The if Statement

The if statement is used to execute a block of code only if the specified condition evaluates to true. This is the simplest JavaScript’s conditional statements and can be written like:

if(condition) {

// Code to be executed

}

The if…else Statement

You can enhance the decision making capabilities of your JavaScript program by providing an alternative choice through adding an else statement to
the if statement.

The if…else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false. It can be written, like this:

if(condition) {

// Code to be executed if condition is true

} else {

// Code to be executed if condition is false

}

The if else Statement

The if…else if…else Statement

The if…else if…else a special statement that is used to combine multiple if…else statements.

if(condition1) {
// Code to be executed if condition1 is true

} else if(condition2) {
// Code to be executed if the condition1 is false and condition2 is true

} else {
// Code to be executed if both condition1 and condition2 are false

}

The if else if else Statement

Using the Switch…Case Statement

The switch..case statement is an alternative to the if…else if…else statement, which does almost the same thing. The switch…case statement tests a variable or expression against a series of values until it finds a match, and then executes the block of code corresponding to that match. It’s syntax is:

switch(x){

case value1:
// Code to be executed if x === value1

break;

           case value2:
// Code to be executed if x === value2

break;

    default:
        // Code to be executed if x is different from all values

}

The switch case statement

The getDay() method returns the weekday as a number from 0 and 6, where 0r represents Sunday.

If you liked our content JavaScript Conditional Statements, then you may also like

Please don’t forget to connect with us through our social media sites. Facebook: Online Notes Nepal, Instagram: @online_notes_nepal, LinkedIn: @onlinenotesnepal, and Twitter:@onlinenotenepal.

advertise here
webtrickshome