H I O X INDIA
Javascript Tutorial
Google
Web hscripts.com
 HOME  ||  Scripts  ||  Purchase  ||  Tutorials  ||  Images  ||  Tools  ||  Directories 
  :-)  Send Page   :-)   Feedback   :-)   Register   :-)   Links   :-)   Support   :-)   Bookmarks :-)  
 Forums   Hosting   Internet Stats   Easy Calculation   FUN Games 

Java-Script Tutorial
Introduction
Variables
Operators
Statements
Loops
Functions
Objects
Arrays
Browser Objects
Form DOM
Ask Your Doubts
Feedback





Ternary Operators


Topic

How to use ternary operator in javascript?



Explanation

Ternary Operator:
As the name indicates ternary operators take three operands. The syntax is condition ? result1 : result2;.

Here you use a condition before question mark (?) followed by result 1 and result 2 separated by a colon (:). Result1 is called if the condition is satisfied else result2 is called.

Example 1:
<script language=javascript>
var b=5;
(b == 5) ? a="true" : a="false";
document.write(" --------------------------- "+a);
</script>


Result:


Example 2:
<script language=javascript>
var b=true;
(b == false) ? a="true" : a="false";
document.write(" --------------------------- "+a);
</script>


Result:




        Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script. Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.

others

privacy policy     license     sitemap
© 2004-2005 HIOX INDIA - hioxindia.com

Other Links