The quick answer is: it makes a difference if the person doesn’t fill in and just call function ()
without passing parameter. In this case it will be undefined
. The null
should be explicitly assigned. The short version of the test would be
if (data != null) {
// Código
}
Full answer
According to my answer in the question (What is the difference between null and Undefined)[/a/2408/280] you can see that the null
is an assigned value and means an instantiated object, already the undefined
is something with an unassigned value and represents a special value in Javascript, the absence of something in the variable. It indicates that a variable has never been defined or that someone has assigned undefined
to clear a variable. If you use typeof
you will see that the object indicates to be of the type "undefined"
.
var a;
console.log(typeof a); //resultado> "undefined"
console.log(typeof a === "object"); //resultado> false
console.log(typeof a === "undefined"); //resultado> true
console.log(typeof a == "undefined"); //resultado> true
Already the null
is a null value assigned to an object. It is used to pass default values of uninitialized objects. If you use typeof
you will see that the object indicates to be of the type "object"
.
var a = null;
console.log(typeof a); //resultado> "object"
console.log(typeof a === "undefined"); //resultado> false
console.log(typeof a === null); //resultado> true
console.log(typeof a == null); //resultado> true
Most of the time you can test using ==
so much for undefined
how much for null
which won’t make a difference, but if you want to make sure it’s something that hasn’t been assigned or if it’s an empty object you should check using ===
by the specific type. Simple comparison (==
) compares only the value and, if necessary, converts the value to the type (case of strings
and numbers) while strict comparison (===
) compares the type and value, without converting it, if the type is not the same it returns false
. See how they behave among themselves:
console.log(false == undefined); //resultado> false
console.log(false == null); //resultado> false
console.log(null == undefined); //resultado> true
console.log(null === null); //resultado> true
console.log(undefined === undefined); //resultado> true
console.log(undefined === null); //resultado> false
console.log(undefined == null); //resultado> true
function test(val) {
return val == null;
}
test(null); //resultado > true
test(undefined); //resultado > true
You can take advantage of this difference when checking by parameters in functions and some of them are optional. Parameters that have not been passed will be of value undefined
and you can accept an empty object with null
. Take an example:
function umaFuncao(primeiro, segundo, opcional) {
if (typeof opcional === "undefined") {
opcional = "três";
}
// faz algo
}
does not forget to check
data != ""
– tissei
@alleen94 If I remember correctly, in this case the argument was supposed to be a
object
, but in the case of strings it is no doubt necessary to check the empty string.– afsantos