Pages

Search This Blog

Wednesday, July 9, 2008

Javascript: Shorten for if/else

Sometime our code will looks messy if containing too many if/else. We can shorten the code and make it more tidy. Check the code below:

<html>
<head>
<script language="JavaScript">
function foo()
{
var myGender = "female";
alert((myGender == "male") ? "i am male" : "I am female");
}
</script>
</head>
<body onload="javascript:foo();">
</body>
</html>

No comments: