🌐

JS比較演算子

2022/10/12に公開

比較演算子

厳格な等価性
型の比較を行う
a === b

抽象的な等価性
型の比較無し
a == b

let a = "1";
let b = 1;

console.log(a === b);
console.log(a == b);

//--実行結果
false
true
function printEquality(a, b) {
  console.log(a === b);
  console.log(a == b);
}

let a = 1;
let c = true;

printEquality(a, c);


//--実行結果
false
true

AND条件OR条件
&& ||


//実行結果全部同じ

function hello(name) {
  if (!name) {
    name = "tom";
  }
  console.log("hello" + name);
}

hello();


function hello(name) {
  name = name || "tom";
  console.log("hello" + name);
}

hello();

function hello(name = "tom") { //デフォルト引数

  console.log("hello" + name);
}

hello();

//
hellotom


let name = "bob"; // ""などの場合は実行されない
name && hello(name); //falseか判定する

//
hellobob

Discussion