-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
219 lines (176 loc) · 5.8 KB
/
main.js
File metadata and controls
219 lines (176 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// このコードはstrict modeで実装される
// strict mode : より安全なコードをかけるように厳しめにチェックしてくれる。
"use strict";
// サンプルコード
// ”こんにちはhibikiwadaさん”と表示されれば成功!
function hello(name) {
console.log("こんにちは" + name + "さん");
}
hello("hibikiwada");
/*
複数行
コメント
はこうやって
記述
*/
/*
constは、再代入できない変数(定数ではない)。
letは、再代入できる。
→基本はconstを使用、再代入する必要がある場合のみletを使用。
「varは時代の遺物。使ったことねぇ!」by 師匠
*/
const name = "和田響";
const sport = "テニス";
let count = 0;
// コンソールでcountを表示
console.log(count);
// コンソールで200+200の結果を表示
const total = 200 + 200;
console.log(total);
/*
データ型 : 値の方(真偽値、数値、文字列など)
データ型はプリミティブ型(基本型)とオブジェクト(複合系)に分けられる。
プリミティブ型:
・真偽値(Boolean):true か false
・数値(Number):2002 とか 3.14
・巨大な整数(Bigint):9999900000みたいなでかい数。
・文字列(string):"和田響"のような文字列。
・undefined;値が未定であることを表すデータ型
・null:値が存在しないことを表すデータ型。
・シンボル(symbol):一意で不変な値のデータ型。
オブジェクト(複合型):
・プリミティブ型以外のデータ型。
・配列、正規表現、dateなど
*/
// typeof 演算子でデータ型を調べる。
console.log(typeof true); //→Boolean
console.log(typeof 999999999999989898989999999999999999999999999999999); //→number
console.log(typeof function () {});
// リテラル:プログラム上で数値や文字列など、データ型の値を直接記述できるように構文として定義されたもの
// ""で囲んだ範囲は文字リテラル→「まじまじまじ」は文字列として認識
const str = "まじまじまじ";
// Number.MAX_SAFE_INTEGER:安全に表せる最大の数値
console.log(Number.MAX_SAFE_INTEGER); //→9007199254740991
// 9007199254740991よりも大きい値にはnをつける→bigintリテラル
const bgi = 90071992547409912n + 2n;
console.log(bgi);
// バックスラッシュで'を文字列として認識させる
const time = '8 o\'clock'
console.log(time); //→8 o'clock
// 2020.0305
// 演算子
/*
演算子:演算処理を記号で表したもの。(+ - *とか)
被演算子(オペランド):演算処理の対象(1+1 の1)
二項演算子:2つのオペランドを取る演算子
*/
const pls = 1 + 5;
console.log(pls); //→6
/*
単項演算子:1つのオペランドを取る演算子(++ --)
*/
let num = 1;
num++;
console.log(num);//==>2
// 文字列結合演算子(+)
console.log("テニス" + "プレーヤー");//==>テニスプレーヤー
// べき乗演算子:(**)
console.log(2 ** 5); //==>32 2の5乗
/*
比較演算子:オペランド同士の値を比較し、真偽値を返す演算子
厳密等価演算子:同じ型で同じ値の時trueを返す
*/
console.log(1 === 1); // ==>true
console.log(1 === "1"); //==>false 型が違う
// 等価演算子(==)はバグが多いからあまり使わない。
// 2021.0306
/*
暗黙的な型変換という概念:明示的はない型変換
*/
// 等価演算子による暗黙的な型変換
// 暗黙的な型変換で面白い結果が出る
console.log(1 == "1"); // ==>true
console.log(0 == false); // ==>true
console.log(10 == ["10"]); // ==>true
/*
コンストラクタ関数(String):文字列へと明示的に変換できる
*プリミティブ型に限る!
*/
String("str"); // ==>"str"
/*
NaN :Not a Number だが、、、
衝撃!!NaNはnumber型
*/
console.log(typeof NaN); // => "number"
// 2021 / 3 / 8
/*
関数宣言は
function(仮引数1,仮引数2) {
関数が呼び出された時の処理
return 返り値;
}
*/
/*
関数の呼び出し
const 関数の結果 = 関数名(引数1,引数2);
console.log(関数の結果);
*/
function double(num) {
return num * 2; // numの2倍の値を返す
}
console.log(double(20)); //==>40
// 関数が何も値を返す必要がない場合は、return文そのものを省略できる
function fn() {
return;
}
console.log(fn()); //undifinde
// 呼び出し次の引数が少ない場合==>undifinedが代入される
function echo(x) {
return x;
}
console.log(echo(1));// ==>1
console.log(echo());// ==>undifined
function argumentsToArray(x, y) {
return [x, y];
}
console.log(argumentsToArray(2, 3)); //==>[2,3]
console.log(argumentsToArray(2)); //==>[2,undifined]
// デフォルト引数
//引数が渡されなかった場合のみデフォルト値が入る
function addPrefix(text, prefix = "デフォルト:") {
return prefix + text;
}
// falsyな値を渡してもデフォルト値は代入されない
console.log(addPrefix("わろた"));//==> デフォルト:わろた
console.log(addPrefix("わろた", "")); //==>わろた
console.log(addPrefix("わろた", "くそ"));//==>くそわろた
// 呼び出し時の引数が多いとき==>無視される
function add(x,y) {
return x + y;
}
console.log(add(1, 2));//==>3
console.log(add(1,2,3));//==>3
//
function userPrintId(user) {
console.log(user.id);
}
const user = {
id: 43
}
userPrintId(user);
// factorialは関数の外から呼び出せる名前
// innerFactは関数の外から呼び出せない名前
const factorial = function innerFact(n) {
if (n === 0) {
return 1;
}
return n * innerFact(n - 1);
}
console.log(factorial(3)); //==>6
// 式
// わからん!!-->わかってきた
const fnc = () => {
return 1;
};
// fn() という式の評価値を表示
console.log(fnc()); // => 1