clover.blue

[JS講座] 3. 型

Data
2019/07/28
Tag

型の種類

文字列(String)

const str = 'こんばんは';
const str2 = "test";
const numStr = '0';

補足

シングルクオートで囲まれてる文字列内でシングルクオートを使いたい場合: '\''
同様にダブルクオートで囲まれてる文字列内でダブルクオートを使いたい場合 : "\""

シングルクオート'、ダブルクオート"どちらも使えますがシングルクオート推奨です。 HTMLでダブルクオートを使うため、区分けをつけるためです。

[デモ]型について

数値(Number)

const num = 0;
const num2 = 1;

論理値(Boolean)

const bool = true;
const bool2 = false;

配列(Array)

const ary = [1, 2, 3];

連想配列(Object)

const obj = {one: 1, two: 2};

型の変換

説明 コード
文字列への変換 ('こんにちは').toString();
数字に変換 parseInt('10');
論理型に変換 Boolean(true);

(10).toString() + 10; //1010
parseInt('10') + 10; //20

[デモ]型の変換