12
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JavaScriptのプリミティブ型と変数の基本まとめ

Posted at

JavaScriptのプリミティブ型と変数の基本まとめ

🟢 プリミティブ型(基本のデータ型)

JavaScriptの主なプリミティブ型は、次の5つです。

説明
Number 数値型 42, 3.14
String 文字列型 "Hello", '世界'
Boolean 真偽値(true / false) true, false
Null 何も値がない null
Undefined 未定義 undefined

🔢 Number型(数値)の例

四則演算や特殊な演算もできます。

演算 サンプル 結果
加算 7 + 13 20
減算 20 - 5 15
乗算 3 * 8 24
除算 30 / 6 5
剰余演算 17 % 4 1
べき乗 2 ** 5 32

🛠 コンソールですぐ試してみよう!

Macなら、ブラウザで
command + option + J
を押すと「開発者ツールのコンソール」を開けます。

下のコードをコンソールに貼り付けてEnterしてみましょう!

console.log(7 + 13);   // 20
console.log(20 - 5);   // 15
console.log(3 * 8);    // 24
console.log(30 / 6);   // 5
console.log(17 % 4);   // 1
console.log(2 ** 5);   // 32

🔍 1行ずつでも、まとめてコピペでも大丈夫です!

💡 NaNとは?

**NaN(Not a Number)**は「数字ではない」ことを表す特別な数値型

例:0 / 0 や 1 + NaN は NaN になる

console.log(0 / 0);    // NaN
console.log(1 + NaN);  // NaN
console.log("abc" - 1); // NaN
console.log(undefined + 1); // NaN

🧐 typeof で型を調べる

typeof 演算子を使うと、変数や値の型が分かります。

console.log(typeof NaN); // "number"

ポイント
NaNも型としては"number"です!

🏷 変数とは?

  • 値に「名前(ラベル)」をつけて、プログラムの中で管理できるようにするしくみです。

  • 変数を使うことで、後からその値を参照したり、計算に使ったりできます。

  • letやvarで宣言した変数は、あとで値を変更することができます(constは変更できません)。

変数の基本構文と値の更新

変数の宣言

let year = 1985; // 変数yearに1985を代入
  • let を使うと「後から値を変更できる変数」を宣言できます

変数の値を更新する方法

let score = 0;     // 初期値0を代入
score = score + 5; // 5を足して、scoreは5になる
score = score + 5; // さらに5を足して、scoreは10になる
score += 5;        // さらに5を足して、scoreは15になる
  • score = score + 5 と score += 5 は同じ意味です
  • += の他にも、-=, *=, /=, %= なども使えます

インクリメント(+1する)

値を「1だけ増やす」専用の書き方です。

score++;    // scoreの値を1増やす(値を表示した後に+1)
++score;    // scoreの値を1増やす(+1してから値を表示)

具体例

let count = 1;

console.log(count++); // 1(表示後に+1されて、countは2になる)
console.log(count);   // 2

let num = 1;
console.log(++num);   // 2(+1されてから表示される)
console.log(num);     // 2

変数の宣言方法

let:値を後から変更できる(推奨)

const:定数(値の再代入不可)

var:昔使われていた。今はほとんど使わない

🔵 Boolean型

true または false のどちらかの値しか取れません。

console.log(10 > 5); // true
console.log(5 > 10); // false
console.log("apple" === "apple"); // true
console.log("1" == 1); // true(型変換が入る!)
console.log("1" === 1); // false(型も一致しないとfalse)

📝 変数の命名規則

let 2value = 10; // ❌ エラー:変数名は数字で始められません
let my-value = 5; // ❌ エラー:記号(-)は使えません
let my_value = 5; // 〇 スネークケース
let myValue = 5;  // 〇 キャメルケース

📝 まとめ

  • JavaScriptのプリミティブ型には、Number・String・Boolean・Null・Undefined の5つがある

  • Number型では四則演算やさまざまな計算ができ、NaN などの特別な値も存在する

  • typeof演算子を使えば、値や変数の型を簡単に調べられる

  • 変数はletやconstで宣言でき、値を後から変更したり保持したりできる

  • Boolean型は true / false のみで、条件分岐や比較に頻繁に使う

  • 変数の命名規則を守ることで、エラーを防げて読みやすいコードになる

12
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?