Skip to main content

Conditional statement in javascript| Alternative of Conditional Statements






Conditional statements control behavior in JavaScript and determine whether or not pieces of code can run. ... “If” statements: where if a condition is true it is used to specify execution for a block of code. “Else” statements: where if the same condition is false it specifies the execution for a block of code.


How to use Conditional Statements

Conditional statements are used to decide the flow of execution based on different conditions. If a condition is true, you can perform one action and if the condition is false, you can perform another action.


Different Types of Conditional Statements

There are mainly three types of conditional statements in JavaScript.

  1. If statement
  2. If…Else statement
  3. If…Else If…Else statement

1) If statement

Syntax:

if (condition)


{


lines of code to be executed if condition is true


}


You can use If statement if you want to check only a specific condition.

Try this yourself:

<html>

<head>

<title>IF Statments!!!</title>

<script type="text/javascript">

var result_percentage = 200

if(result_percentage <=100)

document.write("Congrts <br />");

if(result_percentage >100)

document.write("RESULT Error <br />");

</script>

</head>

<body>

</body>

</html>


2) If…Else statement

Syntax:

if (condition)


{


lines of code to be executed if the condition is true


}


else


{


lines of code to be executed if the condition is false


}


You can use If….Else statement if you have to check two conditions and execute a different set of codes.

Try this yourself:


<html>

<head>

<title>If...Else Statments!!!</title>

<script type="text/javascript">

// Get the current hours

var hours = new Date().getHours();

if(hours<12)

document.write("Good Morning!!!<br />");

else

document.write("Good Afternoon!!!<br />");

</script>

</head>

<body>

</body>

</html>

3) If…Else If…Else statement

Syntax:

if (condition1)


{


lines of code to be executed if condition1 is true


}


else if(condition2)


{


lines of code to be executed if condition2 is true


}


else


{


lines of code to be executed if condition1 is false and condition2 is false


}


You can use If….Else If….Else statement if you want to check more than two conditions.

Try this yourself:


<html>

<head>

<script type="text/javascript">

var one = 10

var two = 20

one = parseInt(one);

two = parseInt(two);

if (one == two)

document.write(one + " is equal to " + two + ".");

else if (one<two)

document.write(one + " is less than " + two + ".");

else

document.write(one + " is greater than " + two + ".");

</script>

</head>

<body>

</body>

</html>


Alternative of Conditional Statements:

Conditional (ternary) operator

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally, the expression to execute if the condition is falsy. This operator is frequently used as a shortcut for the if statement.

Syntax:-

condition ? exprIfTrue : exprIfFalse


Parameters

condition
An expression whose value is used as a condition.
exprIfTrue
An expression which is evaluated if the condition evaluates to a truthy value (one which equals or can be converted to true).
exprIfFalse
An expression which is executed if the condition is falsy (that is, has a value which can be converted to false).

Description

Besides false, possible falsy expressions are: null, NaN, 0, the empty string (""), and undefined. If condition is any of these, the result of the conditional expression will be the result of executing the expression exprIfFalse.

Examples

A simple example

var age = 26;
var beverage = (age >= 21) ? "Beer" : "Juice";
console.log(beverage); // "Beer"

function getFee(isMember) {

  return (isMember ? '$2.00' : '$10.00');

}

console.log(getFee(true));

// expected output: "$2.00"

console.log(getFee(false));

// expected output: "$10.00"

console.log(getFee(null));

// expected output: "$10.00"


Comments

Popular posts from this blog

How to create and activate an AWS account - Amazon AWS in Hindi/English

aws with mukund,create and activate aws,how to aws configure,aws tutorial,aws tutorial in hindi,quick aws tutorial,aws quicksight,mukund programming tutorials,amazon web services,aws account creation,aws account,create aws account,aws tutorial for beginners in hindi,how to create aws account,aws cloud,aws training,how to,free aws account,free aws,aws with mukunda,how to configure aws cli in windows,how to configure aws cli on mac

What are the data types and variables in JavaScript? and example of sum operation on hindi variables.

Datatypes in JavaScript There are majorly two types of languages. First, one is  Statically typed language  where each variable and expression type is already known at compile time. Once a variable is declared to be of a certain data type, it cannot hold values of other data types. Example: C, C++, Java.  // Java(Statically typed) int x = 5   // variable x is of type int and it will not store any other type. string y = 'abc' // type string and will only accept string values Other,  Dynamically typed languages : These languages can receive different data types over time. For example- Ruby, Python, JavaScript etc. // Javascript(Dynamically typed) var x = 5; // can store an integer var name = 'string' ; // can also store a string. JavaScript is a dynamically typed (also called loosely typed) scripting language. That is, in JavaScript variables can receive different data types over time. Datatypes are basically typed data that can be used and manipulated in a pr...