JavaScript Tutorial/Operators/Conditional Operator

Материал из Web эксперт
Перейти к: навигация, поиск

Conditional Operator: ?

   <source lang="javascript">

<html> <head> <title>Conditional Operator Demo</title> <script language="javascript" type="text/javascript">

</script> </head> <body>

JavaScript Test Page

</body> </html></source>


Finding the absolute value

   <source lang="javascript">

<html> <head> <title>Finding the absolute value</title> <script language="javascript" type="text/javascript">

</script> </head> <body>

Finding the absolute value

<a href="javascript:f()">Get absolute value</a> </body> </html></source>


Tenary operator

   <source lang="javascript">

<html> <head> <title>A Simple Page</title> <script language="JavaScript">

</script> </head> <body> </body> </html></source>


The Conditional Operator and if Statement

An expression that evaluates to a Boolean is always placed to the left of the question mark (?) in the Conditional Operator.

If the expression evaluates to true, the value between the question mark and the colon (:) is returned.

If the expression evaluates to false, the value following the colon is returned.



   <source lang="javascript">

<html> <SCRIPT LANGUAGE="JavaScript">

</SCRIPT> </html></source>