if...else

種類命令
書式
if (条件) 条件を満たした場合の処理

if (条件) 条件を満たした場合の処理 else 条件を満たさなかった場合の処理

if (条件)
{
条件を満たした場合の処理
}

if (条件)
{
条件を満たした場合の処理
}else{
条件を満たさなかった場合の処理
}
解説
条件を満たした場合にif()以下を実行します。else以後は、条件を満たしていない場合の処理を行います。ifは複数入れ子にする(ネスト)ことができます。
サンプルコード
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Sample</title>
<style type="text/css"><!--
body { margin:0px; }
form { position:absolute; top:10;left:10px; }
--></style>
<script type="text/javascript"><!--
window.onload = function()
{
n = (new Date()).getSeconds();
if (n < 30)
{
document.getElementById("txtField").value = "30未満です";
}else{
document.getElementById("txtField").value = "30以上です";
}
}
// --></script>
</head>
<body>
<img src="Default.png">
<form>
<input type="text" id="txtField" size=40"><br>
</form>
</body>
</html>
実際のスクリプトをダウンロード