I felt that I just contribute to content on the JavaScript's null and undefined keywords, which I feel might be helpful to especially some new JavaScript programmers that seek to understand these keywords. Below I took time to compare these two, using some if-statements to do some things if conditions translated to true, and do otherwise, something else if translated to false. NB: When a variable is just declared without being assigned any value, JavaScript assigns undefined to that variable, where as null is usually always assigned by a programmer; I prove the first part of this note, hereinbefore, in the last console.log()-statement which translates to the Boolean value, true.
let firstValue = null,
secondValue; // Declared without value assignment.
if (firstValue === secondValue) {
console.log('"null" is explicitly equal to "undefined".');
} else {
console.log('"null" is NOT explicitly equal to "undefined".');
}
if (firstValue == secondValue) {
console.log('"null" is somewhat equal to "undefined".');
} else {
console.log('"null" is NOT equal to "undefined" at all!');
}
if (firstValue) {
console.log('"null" is truthy.');
} else {
console.log('"null" is falsey.');
}
if (secondValue) {
console.log('"undefined" is truthy.');
} else {
console.log('"undefined" goes to falsey.');
}
console.log(undefined === secondValue);
Below is the result of the above code:
"null" is NOT explicitly equal to "undefined". main.js:11
"null" is somewhat equal to "undefined". main.js:19
"null" is falsey. main.js:25
"undefined" goes to falsey. main.js:28
true //Boolean Value.
FACT: Both values translate to false, but are not explicitly equal to each other.
Aucun commentaire:
Enregistrer un commentaire