LoginSignup
0
1

More than 3 years have passed since last update.

javascript超基礎

Posted at

型の名前 種類 特徴
プリミティブ型 文字列,数値,null,真偽値,undefined 値渡し
オブジェクト型 配列,オブジェクト 参照渡し

変数と定数

  • 最近のプログラミングでは、なるべくconstを使いつつ、必要なときにletを使う。
    • (varは古い)
  • 使える文字は英数字、$、_のみ。
    • (ハイフンは使えない)
  • 数値から始められない。
  • JavaScriptでは大文字と小文字は区別される。
  • 予約語は使えない。
  • constで定義しても配列なら上書きできる。

値の再代入

let price = 500;

// price = price + 100;
price += 100; // 600

// price = price * 2;
price *= 2; // 1200

// price = price + 1;
// price += 1;
price++; // 1201

// price -= 1;
price--; // 1200

JavaScriptデータ型

  • 文字列(String)
  • 数値(Number)
  • Undefined
  • Null
  • 真偽値(Boolean)
  • オブジェクト(Object)

※nullをtypeof関数でチェックするとobjectと認識されるのはJavaScriptの有名なバグ

数値からなる文字列

  • 「+」は文字列の連結のための演算子となる
  • 足し算にしたい場合はparseInt()で数値に変換する。
console.log('5' * 3); //15
console.log('5' - '3'); //2

console.log('5' + 3); //53
console.log(parseInt('5', 10) + 3); //8

console.log(parseInt('hello', 10)); //NaN

真偽値

false:0,null,undefined,'',false
true:上記以外

テンプレートリテラル

変数の値を文字列中に埋め込む機能。他の言語ではよく見られる機能。

for (let i = 0; i <= 10; i++) {
    console.log(`hello ${i}`);
}

関数

仮引数にはletやconstなどの宣言は不要。

function showAd(message = 'Ad') { // 仮引数
    console.log('----------');
    console.log(`--- ${message} ---`);
    console.log('----------');
}

関数宣言と関数式

関数式は関数を定数や変数に値として代入するため、最後に;が必要。
関数式で渡す関数には名前がないため、無名関数とよく呼ばれる。

//関数宣言
function 関数名(仮引数,仮引数) {
    処理;
    return 返り値;
}
//呼び出し
関数名(実引数,実引数);

//関数式
const 定数名 = function(仮引数,仮引数) {
    処理;
    return 返り値;
};
//呼び出し
定数名(実引数,実引数);
0
1
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
1