JavaScriptの出力方法
プログラミング学習の第一歩は、プログラムに「話しかける」ことです。JSには4つの出力方法があり、それぞれに異なる用途があります。
console.log() — 開発者の親友
console.log()はブラウザのコンソール(F12 → Console)に内容を出力します。ユーザーには見えませんが、デバッグには欠かせないツールです。
これは圧倒的に最もよく使う出力方法です。バグの追跡也好検証也好、console.log()が頼りになるツールになります。
alert() — 使いすぎ注意
alert()はユーザーが「OK」をクリックして閉じる必要があるモーダルダイアログを開きます。ユーザーの作業フローを中断するため、本番環境で使うことはほぼありません。主に学習段階で登場します。
注意:デバッグ中にalert()をループ内に入れないでください。ダイアログを閉じるために何十回も「OK」をクリックしなければならなくなります — どうしてわかるかは聞かないでください。
document.write() — テスト専用
document.write()はHTMLドキュメントストリームに直接コンテンツを書き込みます。ページの読み込み完了後に呼び出すと、ページ全体が上書きされます — これはほぼ望ましい動作ではありません。簡単な実験にだけ使いましょう。
innerHTML — ページコンテンツの更新
innerHTMLが「ページに出力する」標準的な方法です。要素を選択してHTMLコンテンツを変更します。これはDOM操作の基礎であり、後ほど詳しく扱います。
実例:console.log()でコンソールに出力
HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>JS出力 - console.log</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; margin-top: 80px; }
.tip { background: #e3f2fd; padding: 16px; border-radius: 8px; display: inline-block; color: #1565C0; }
#output { margin-top: 20px; font-size: 18px; color: #333; white-space: pre-line; }
button { margin-top: 16px; padding: 10px 24px; font-size: 16px; border: none; border-radius: 6px; background: #2196F3; color: #fff; cursor: pointer; }
button:hover { background: #1976D2; }
</style>
</head>
<body>
<p class="tip">📌 F12キーでコンソールを開き、console.logの出力を確認しましょう</p>
<button id="logBtn">コンソールにログ出力</button>
<p id="output"></p>
<script>
document.getElementById("logBtn").onclick = function() {
console.log("こんにちは、コンソール!");
console.log("現在時刻: " + new Date().toLocaleString());
console.log("1 + 2 =", 1 + 2);
var outputEl = document.getElementById("output");
outputEl.textContent = "コンソールに3つのメッセージを出力しました — F12で確認してください!";
outputEl.style.color = "#4CAF50";
};
</script>
</body>
</html>
実例:alert()ダイアログ出力
HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>JS出力 - alert</title>
<style>
body { font-family: Arial, sans-serif; text-align: center; margin-top: 80px; }
.warn { background: #fff3e0; padding: 14px; border-radius: 8px; display: inline-block; color: #E65100; margin-bottom: 20px; }
button { padding: 10px 24px; font-size: 16px; border: none; border-radius: 6px; cursor: pointer; color: #fff; margin: 6px; }
#alertBtn { background: #FF9800; }
#safeAlertBtn { background: #4CAF50; }
</style>
</head>
<body>
<p class="warn">⚠️ alert()はUIをブロックします — 実際のプロジェクトでの使用は避けましょう</p>
<br>
<button id="alertBtn">alertを表示</button>
<button id="safeAlertBtn">ページ内メッセージ(推奨)</button>
<p id="msg" style="margin-top:20px; font-size:20px; color:#4CAF50;"></p>
<script>
document.getElementById("alertBtn").onclick = function() {
alert("これはalertダイアログです!続行するにはOKをクリックしてください。");
};
document.getElementById("safeAlertBtn").onclick = function() {
document.getElementById("msg").textContent = "これはページ内メッセージです — 中断なしで、はるかに良いUXです!";
};
</script>
</body>
</html>
実例:innerHTMLでページコンテンツを更新
HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>JS出力 - innerHTML</title>
<style>
body { font-family: Arial, sans-serif; max-width: 500px; margin: 60px auto; text-align: center; }
#content { min-height: 80px; padding: 20px; border: 2px dashed #ccc; border-radius: 8px; font-size: 18px; color: #666; }
button { margin: 8px; padding: 10px 24px; font-size: 16px; border: none; border-radius: 6px; cursor: pointer; color: #fff; }
#textBtn { background: #9C27B0; }
#htmlBtn { background: #e91e63; }
#clearBtn { background: #999; }
</style>
</head>
<body>
<h2>innerHTML出力</h2>
<div id="content">JSの出力がここに表示されます</div>
<button id="textBtn">テキスト出力</button>
<button id="htmlBtn">HTML出力</button>
<button id="clearBtn">クリア</button>
<script>
var contentEl = document.getElementById("content");
document.getElementById("textBtn").onclick = function() {
contentEl.innerHTML = "これはプレーンテキストの出力です";
contentEl.style.color = "#9C27B0";
};
document.getElementById("htmlBtn").onclick = function() {
contentEl.innerHTML = "<strong style='color:#e91e63;'>太字テキスト</strong>と<em>斜体テキスト</em> — innerHTMLはHTMLタグを解釈できます";
contentEl.style.color = "#333";
};
document.getElementById("clearBtn").onclick = function() {
contentEl.innerHTML = "";
contentEl.style.color = "#666";
};
</script>
</body>
</html>
実例:document.write()とその危険性
HTML
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>JS出力 - document.write</title>
<style>
body { font-family: Arial, sans-serif; max-width: 500px; margin: 60px auto; text-align: center; }
.danger { background: #ffebee; padding: 14px; border-radius: 8px; color: #c62828; margin-bottom: 20px; }
button { margin: 8px; padding: 10px 24px; font-size: 16px; border: none; border-radius: 6px; cursor: pointer; color: #fff; }
#safeBtn { background: #4CAF50; }
#dangerBtn { background: #f44336; }
</style>
</head>
<body>
<p class="danger">⚠️ ページ読み込み後にdocument.write()を呼び出すと、ページ全体が上書きされます!赤いボタンで動作を確認できます(更新で元に戻ります)</p>
<button id="safeBtn">安全な出力(innerHTML)</button>
<button id="dangerBtn">危険な出力(document.write)</button>
<p id="safeOutput" style="margin-top:20px; font-size:18px; color:#4CAF50;"></p>
<script>
document.getElementById("safeBtn").onclick = function() {
document.getElementById("safeOutput").textContent = "innerHTML経由の出力 — ページはそのまま残ります!";
};
document.getElementById("dangerBtn").onclick = function() {
document.write("<h1 style='text-align:center; color:red; margin-top:100px;'>ページ全体が上書きされました!</h1><p style='text-align:center;'>これがdocument.write()の危険性です — 更新で元に戻ります</p>");
};
</script>
</body>
</html>
📖 まとめ
console.log()は主要なデバッグツール — ユーザーには見えないコンソールに出力するalert()はUIをブロックする — 学習時のみ使い、本番環境では使わないdocument.write()は読み込み後に呼び出すとページ全体を上書きする — 初期の実験にのみ適しているinnerHTMLはページコンテンツを更新する標準的な方法で、HTMLタグを解釈できる- 実際の開発では、99%の場合は
console.log()でデバッグし、innerHTMLでページを更新する - それぞれの方法には適切な場面がある — 正しく選べば、よりスマートに作業できる
❓ よくある質問
Q ユーザーはconsole.log()の出力を確認できますか?
A いいえ — 開発者ツールのコンソールに出力されます。ダッシュボードの裏側にある計器盤のようなもので、ドライバーには見えませんが整備士には不可欠です。重要な情報をconsole.logだけに入れないようにしましょう — ユーザーには見えません。
Q innerHTMLとtextContentの違いは何ですか?
A
innerHTMLはHTMLタグを解釈しますが、textContentはすべてをプレーンテキストとして扱います。例えば<strong>太字</strong>を設定した場合、innerHTMLは太字テキストを表示しますが、textContentはタグの文字列をそのまま表示します。テキストだけを変更する場合はtextContentの方が安全です — 注入されたスクリプトが誤って実行される心配がありません。Q なぜalert()は推奨されないのですか?
A ページをフリーズさせるためです — ユーザーはダイアログを閉じるまで何もできません。フォーム入力中に各フィールドの後ろにポップアップが表示される状況を想像してください — ひどい体験になるでしょう。
📝 演習
- 初級:ボタンを持つページを作成し、クリック時に
console.log()であなたの名前と年齢を出力してください。 - 中級:
innerHTMLを使って順序なしリスト(3項目以上)を出力するページを作成してください。各項目は学習したプログラミング言語にしてください。 - 上級:4つの出力方法を比較するページを作成してください。4つのボタンを用意し、それぞれが4つの出力方法の1つに対応するようにします。クリックすると、ページ上の同じエリアにその方法の説明を表示してください(alertとdocument.writeはスキップして、innerHTMLでシミュレートしてください)。



