基本代入演算子(=)を実装する
このステップでは、JavaScriptの基本代入演算子(=)について学びます。代入演算子は、変数に値を割り当てるために使用され、データを格納して操作する基本的な方法を提供します。
前のステップで作成したassignment-operators.html
ファイルを開きます。<script>
タグの中に、基本代入を探求するための次のJavaScriptコードを追加します。
// Declaring and assigning variables using the = operator
let firstName = "John";
let age = 25;
let isStudent = true;
// Demonstrating variable assignment
console.log("First Name:", firstName);
console.log("Age:", age);
console.log("Is Student:", isStudent);
重要な概念を分解してみましょう。
let
は最新のJavaScriptで変数を宣言するために使用されます
=
演算子は変数に値を割り当てます
- 変数はさまざまな型のデータを格納できます:文字列、数値、およびブール値
console.log()
はブラウザのコンソールに値を表示するために使用されます
同じ演算子を使用して変数を再割り当てることもできます。
// Reassigning variable values
age = 26;
console.log("Updated Age:", age);
// Assigning the value of one variable to another
let originalAge = age;
console.log("Original Age:", originalAge);
ブラウザのコンソールでの例の出力:
First Name: John
Age: 25
Is Student: true
Updated Age: 26
Original Age: 26