LoginSignup
0
0

More than 1 year has passed since last update.

【Javascrip】リテラルとは

Posted at

自分用学習メモ


リテラルとは

リテラルとは、プリミティブ型や一部のオブジェクト型に格納できる値の事

const str = 'hello'; //文字列リテラル

const x = 123; //数値リテラル 

プリミティブ型とは

プリミティブ型とは、リテラル値(変数の値)が規定値以外のものに変更できないデータ型の事。

数値、文字列、論理値、長整数、null、undefined、シンボルがある。


整数リテラルを表示する

// 0bから始まる2進数リテラル

console.log(0b0110); // => 6

// 0xから始まる16進数リテラル

console.log(0xF); => 15

浮動小数点リテラル

.123; // => 0.123

2e8; // => 200000000

テンプレートリテラル

テンプレートリテラルは、バッククォート(`)で囲んだ範囲を文字列として扱える。

let x = 123;
console.log(`次の値は${x}です。`);

//次の値は123です。

UndefinedはGlobal変数(関数内で定義できるが非推奨)

function fn(){
    const undefined = "test";
    console.log(undefined);
}
fn();
// test

オブジェクトリテラル

const obj = {
  'tomato':300,
  'apple' :200
}
//console.log(obj);
console.log(obj.tomato);    //ドット記法
console.log(obj['apple']);   //ブラケット記法

オブジェクトリテラルでは数値から始まる識別子は利用できない(ドット記法)

const obj = {
  '123':300
}
console.log(obj.123);
// SyntaxError

配列リテラル

const array      = [1,2,3];
console.log(array[0]);
// 1

※ JavaScriptにおける数値の型は、「IEEE754倍精度浮動小数点数(64ビット)」と1種類のみなため
JSにおける数値は少数で表現される。

IEEE754倍精度浮動小数点数
参考サイト

0
0
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
0
0