戻る

値が存在しない状態(nullとundefined)
値が存在しない状態

null

意図的に値がないことを示す特殊な値です。
オブジェクトの存在しない状態を表すために使用されます。
論理演算では偽値として扱われます。
値が意図的に「空」であることを示すために使用されます。
オブジェクトのプロパティが今は存在しないことを表す場合などに使われます。

copy
function test1(value)
{
	if(value == null)
	{
		console.log("value(==):"+value);//value(==):null
	}
	if(value === null)
	{
		console.log("value(===):"+value);//value(===):null
	}
	let tmp;//undefined
	if(tmp ==  null)
	{
		console.log("tmp(undefined) ==");//tmp(undefined) ==
	}
	if(tmp ===  null)
	{
		console.log("tmp(undefined) ===:true");//
	}
	else
	{
		console.log("tmp(undefined) ===:false");//tmp(undefined) ===:false
	}
}
test(null);
===(厳密等価演算子)
変数とnullを比較判定した場合、
===(厳密等価演算子)はtrueを返します。
==(等価演算子)
変数とnullを比較判定した場合、
==(等価演算子)はtrueを返します。
また、変数がundefinedであった場合、
==(等価演算子)とnullを比較判定した場合もtrueを返します。

undefined
変数が宣言されているが、まだ値が代入されていない状態を表します。
また、関数が値を返さない場合に暗黙的に返される値でもあります。

copy
function test2()
{
	let value;//undefined

	if(value == undefined)
	{
		console.log("value(==):"+value);//value(==):undefined
	}
	if(value === undefined)
	{
		console.log("value(===):"+value);//value(===):undefined
	}

	if(value ==  null)
	{
		console.log("value(undefined) ==");//value(undefined) ==
	}
	if(value ===  null)
	{
		console.log("value(undefined) ===:true");//
	}
	else
	{
		console.log("value(undefined) ===:false");//value(undefined) ===:false
	}
}
test2();
===(厳密等価演算子)
変数とundefinedを比較判定した場合、
===(厳密等価演算子)はtrueを返します。
==(等価演算子)
変数とundefinedを比較判定した場合、
==(等価演算子)はtrueを返します。
==(等価演算子)とnullを比較判定した場合もtrueを返します。

関数の引数チェック
JavaScriptには、他のプログラミング言語のように引数がnullになることを明示的に禁止する仕組みはありません。
そのため、下記のチェックを行うことで回避を処理を実装します。
(1)関数の引数にデフォルト値を設定
(2)引数の存在チェック

関数の引数にデフォルト値を設定
JavaScriptでは、関数の引数にデフォルト値を設定できます。
引数が省略された場合、またはundefinedの場合、デフォルト値が使用されます。
もしも引数にnullやnullが入った変数を設定した場合でも、デフォルト値により回避可能となります。

copy
function test3(param = "default")
{
	console.log("param:"+param);
}
[実行例]
test3();//param:default
test3(null);//param:default
test3();//param:default
test3("value");//param:value

引数の存在チェック
関数内部でnullチェックをする例
copy
function test4(value)
{
	if(value === null || value === undefined)
	{
		value = "default";
	}
	console.log("value:"+value);
}
test4(null);
引数がnullかundefinedかチェックし、該当する場合は別の処理を行うサンプルです。



戻る
back

Absence of a value (null and undefined)
Absence of a value

null

A special value that indicates the intentional absence of a value.
Used to indicate the non-existence of an object.
Treats as a false value in logical operations. 
Used to indicate that a value is intentionally "empty".
Used to indicate that a property of an object does not currently exist. 

copy
function test1(value)
{
	if(value == null)
	{
		console.log("value(==):"+value);//value(==):null
	}
	if(value === null)
	{
		console.log("value(===):"+value);//value(===):null
	}
	let tmp;//undefined
	if(tmp ==  null)
	{
		console.log("tmp(undefined) ==");//tmp(undefined) ==
	}
	if(tmp ===  null)
	{
		console.log("tmp(undefined) ===:true");//
	}
	else
	{
		console.log("tmp(undefined) ===:false");//tmp(undefined) ===:false
	}
}
test(null);
=== (strict equality operator)
When comparing a variable with null,
=== (strict equality operator) returns true.
== (equality operator)
When comparing a variable with null,
== (equality operator) returns true.
Also, if the variable is undefined,
== (equality operator) also returns true when comparing with null.

undefined
Denotes that a variable has been declared but has not yet been assigned a value.
It is also the value implicitly returned if a function does not return a value.

copy
function test2()
{
	let value;//undefined

	if(value == undefined)
	{
		console.log("value(==):"+value);//value(==):undefined
	}
	if(value === undefined)
	{
		console.log("value(===):"+value);//value(===):undefined
	}

	if(value ==  null)
	{
		console.log("value(undefined) ==");//value(undefined) ==
	}
	if(value ===  null)
	{
		console.log("value(undefined) ===:true");//
	}
	else
	{
		console.log("value(undefined) ===:false");//value(undefined) ===:false
	}
}
test2();
=== (strict equality operator)
When comparing a variable with undefined,
=== (strict equality operator) returns true.
== (equality operator)
When comparing a variable with undefined,
== (equality operator) returns true.
== (equality operator) also returns true when comparing with null.

Function argument check
JavaScript does not have a mechanism to explicitly prohibit arguments from becoming null, as in other programming languages.
Therefore, the following checks are performed to implement the avoidance process.
(1)Set default value for function argument
(2)Check existence of argument

Set default value for function argument
JavaScript allows you to set default value for function argument.
If argument is omitted or undefined, default value is used.
Even if null or a variable containing null is set as argument, it can be avoided by default value.

copy
function test3(param = "default")
{
	console.log("param:"+param);
}
[Example]
test3();//param:default
test3(null);//param:default
test3();//param:default
test3("value");//param:value

Checking whether an argument exists
Example of checking for null within a function
copy
function test4(value)
{
	if(value === null || value === undefined)
	{
		value = "default";
	}
	console.log("value:"+value);
}
test4(null);
This is an example that checks whether an argument is null or undefined, and performs a different process if it is.


back



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