Health & Medical Eating & Food

Harsch Fermenting Crock



There is one further way if which you can perform comparisons in JavaScript and that is using what is known as the ternary operator. It is called the ternary operator because unlike all the operators where the opterator goes between two values, this operator actually requires a third value to be placed in the middle of the operator.

The ternary operator is a substitute for an if statement where both the if and else clauses assign different values to the same field - like this:



if (condition)
  result = 'something';
else
  result = 'somethingelse';


The ternary operator shortens this if/else statement into a single statement.

result = (condition) ? 'something' : 'somethingelse';


While this particular substitution can only be done where the original if statement follows the format shown above this is in fact one of the more common situations where an if statement is needed and the ternary operator is the more efficient way of coding this construct since when coded using the ternary operator it is obvious that one of two values is to be loaded into the result field where the content of an if statement can be anything at all.

Note that you can nest ternary operators since the second and third positions can have anything that returns a value substituted for them.

Note that to test this example try adding a ? followed by a colour to the end of the address of the page - example13.html?red

HTML


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<title>Example 13</title>
</head>
<body>
<p id="ex"></p>
<script type="text/javascript" src="example13.js"></script>
</body>
</html>



JavaScript


var colour, favcol;
colour = window.location.search.substring(1);
favcol = ('green' === colour) ? 'is green.' : 'is not green.';
document.getElementById('ex').innerHTML = colour + favcol;

SHARE
RELATED POSTS on "Health & Medical"
Effective Tips For Buying Catering Equipment
Effective Tips For Buying Catering Equipment
Carrot & Parsnip Latkes Recipe
Carrot & Parsnip Latkes Recipe
Grand Marnier Truffle Cocktail Recipe
Grand Marnier Truffle Cocktail Recipe
Instructions for an Al Dente Pasta Machine
Instructions for an Al Dente Pasta Machine
How to Get a Pound Cake Out of the Pan
How to Get a Pound Cake Out of the Pan
Chinese Restaurants in Menomonee Falls, WI
Chinese Restaurants in Menomonee Falls, WI
Crab cakes and other lip smacking Recipes from Sea
Crab cakes and other lip smacking Recipes from Sea
How To Choose Your Contract Beverage Manufacturers
How To Choose Your Contract Beverage Manufacturers
Traditional Serbian-Croatian Salt Cod with Potatoes
Traditional Serbian-Croatian Salt Cod with Potatoes
Excellent Soft Serve Rental Services in Utah
Excellent Soft Serve Rental Services in Utah
Top 5 Korean Cookbooks Written in English
Top 5 Korean Cookbooks Written in English
Larry's Pizza
Larry's Pizza
A Yummy Pizza Recipe to make your mouth water
A Yummy Pizza Recipe to make your mouth water
Weber Spirit E-310 Gourmet BBQ System Gas Grill Review
Weber Spirit E-310 Gourmet BBQ System Gas Grill Review
Brewing Your Own Beer Flavors From Various Beer Recipes
Brewing Your Own Beer Flavors From Various Beer Recipes
How to Cure Soapstone Pizza Stone
How to Cure Soapstone Pizza Stone
How to Make a Kiwi Banana Smoothie
How to Make a Kiwi Banana Smoothie
Easy Moroccan Whole Almond Macaroons
Easy Moroccan Whole Almond Macaroons
Edible Cake Decorations - Advantages Of Making Your Most Treasured Edible Delicacies
Edible Cake Decorations - Advantages Of Making Your Most Treasured Edible Delicacies
Convert Juicy Plums into a Tangy and Sweet Plum Jam
Convert Juicy Plums into a Tangy and Sweet Plum Jam

Leave Your Reply

*