戻る

厳密な型変換による判定
===(厳密等価演算子)と==(抽象等価演算子)の判定について

[サンプル]
copy
console.log("===(厳密等価演算子)を使用したテスト");
console.log("結果を数字で判定");
resultOfDivTest1(10, 2, 0);
console.log("結果を文字列で判定");
resultOfDivTest1(10, 2, "0");
function resultOfDivTest1(num1, num2, judge)
{
	if(num1 % num2 === judge)
	{
		//trueのケース
		console.log("true");
	}
	else
	{
		//falseのケース
		console.log("false");
	}
}

console.log("==(抽象等価演算子)を使用したテスト");
console.log("結果を数字で判定");
resultOfDivTest2(10, 2, 0);
console.log("結果を文字列で判定");
resultOfDivTest2(10, 2, "0");
function resultOfDivTest2(num1, num2, judge)
{
	if(num1 % num2 == judge)
	{
		//trueのケース
		console.log("true");
	}
	else
	{
		//falseのケース
		console.log("false");
	}
}
/*
[結果]

===(厳密等価演算子)を使用したテスト
結果を数字で判定
true
結果を文字列で判定
false
==(抽象等価演算子)を使用したテスト
結果を数字で判定
true
結果を文字列で判定
true
*/

コーディングをすると判定箇所を変数にして動的に判定をするため
===(厳密等価演算子)を使った判定にしないと、疲れているときなどは、間違いに気づかないまま
単体テストなどを通過してしまい、あとから大変なことになることがあります。
このようなバグに注意するためにも、
意図した結果と異なる可能性があるため、===(厳密等価演算子)を使用することが推奨されます。
== (抽象等価演算子)
比較する際、自動的に型を変換して比較します。
10を2で割った剰余は0ですよね。
この例だと、"0" == 0 は true になります。
これは文字列 "0" が数値の 0 に変換されて比較されるためです。
この自動的な型変換は、予期せぬ結果を生む可能性があり、バグの原因になることがあります。

=== (厳密等価演算子)
=== は型変換を行わずに、値と型を厳密に比較します。
"0" === 0 は false になります。
なぜなら、文字列と数値は型が異なるため、=== では等しくないと判断されるからです。



戻る
back

Strict type conversion
About determining === (strict equality operator) and == (abstract equality operator)

[sample]
copy
console.log("Testing with === (strict equality operator)");
console.log("Results are judged numerically");
resultOfDivTest1(10, 2, 0);
console.log("Determine the result by string");
resultOfDivTest1(10, 2, "0");
function resultOfDivTest1(num1, num2, judge)
{
	if(num1 % num2 === judge)
	{
		//True Case
		console.log("true");
	}
	else
	{
		//The false case
		console.log("false");
	}
}

console.log("Testing with == (abstract equality operator)");
console.log("Results are judged numerically");
resultOfDivTest2(10, 2, 0);
console.log("Determine the result by string");
resultOfDivTest2(10, 2, "0");
function resultOfDivTest2(num1, num2, judge)
{
	if(num1 % num2 == judge)
	{
		//True Case
		console.log("true");
	}
	else
	{
		//The false case
		console.log("false");
	}
}
/*
[result]

Test using === (strict equality operator)
Test the result as a number
true
Test the result as a string
false
Test using == (abstract equality operator)
Test the result as a number
true
Test the result as a string
true
*/

When coding, variables are used to dynamically evaluate variables.
If you don't use the === (strict equality operator) for the evaluation, you may make a mistake when you're tired and pass a unit test without realizing it.
This can lead to serious problems later.
To avoid bugs like this,
it is recommended to use the === (strict equality operator) because the results may differ from what you intended.
== (Abstract Equality Operator)
When comparing, the type is automatically converted.
The remainder when 10 is divided by 2 is 0, right?
In this example, "0" == 0 is true.
This is because the string "0" is converted to the numeric value 0 for comparison.
This automatic type conversion can produce unexpected results and can cause bugs.

=== (strict equality operator)
=== strictly compares the value and type without type conversion.
"0" === 0 is false.
This is because strings and numbers are different types, so === determines they are not equal.



back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。