If I attempt to call an object property that I expect is a function, but turns out to be undefined, that can result in an Error that is the result of calling undefined. This can often be the case when choosing to go with function expressions rather than declarations and neglect to do what is required to keep that from happening, or it could just be a simple typo.
When working with functions a value of undefined is what is returned by a function by default unless something else is returned by using the return keyword. This might be a good thing in some cases as undefined will evaluate to false, so when it comes to functions that return a boolean value it might not always present a problem. Still it might be a good idea to have the function return false anyway just for the sake of making things explicit.
There is also the undefined keyword that can be used to intentionally set a variable to undefined, and can also be used in expressions when it comes to testing for undefined. That is that I often find myself using the undefined keyword as a way to test for undefined by combining the undefined keyword with an identity operator and a value that is to be tested for undefined.
- The examples in this post are on github
1.1 - when a variable is declared, but not assigned anything.
1.2 - undefined is what is returned by a function by default
When writing a function that returns some kind of result, if nothing is returned then the default value that is returned is undefined. To have a function return something other than undefined the return keyword can be used inside the body of a function to define what it is that is to be returned.
1.3 - undefined is the default value for an argument
When writing a function that accepts one or more arguments if an argument is not given then the default value for the argument is undefined. So testing for undefined is often used as a way to determine of no argument is given,and then thus set a default for that argument when writing a function.
2.1 - It may just be a simple typo
2.2 - Use of function expressions can result in calling undefined if used carelessly
So then there are two general ways to avoid this, if you are going to use expressions just make sure that they are set to the variable, of object property that you are calling them off first before calling them. The other way to avoid this is to not use function expressions, but function declarations in place of expressions. When using declarations the function can actually be called above the array at which it was defined, which is of course one major talking point when it comes to the differences between function expressions and declarations.
One of the weird things about the isNaN method is that it will return true for some values that are not NaN including the undefined value. Because of this there are often isNaN methods in various frameworks besides the fact that there is a native method for doing so that do a better job of finding out if a value is nan or not.
4 - The undefined literal or keyword
There is also the undefined keyword or the undefined literal as it might sometimes be called. This is often used as a way to test for undefined by way of an expression using the identity operator. I can not say that there are many instances in which I set a variable or property to undefined as there are other ways to do what is often the case with that. Also I do not see the point of passing undefined as an argument as that is the default value for an argument after all. Still for the most part I find myself using a undefined literal now and then so in this section I will be going over some code examples of this.
4.1 - A Basic example of the undefined literal
Whenever I use the undefined literal it is just abut always for the sake of testing for an undefined value. This is done by using the undefined literal as one of the operands of the identity operator, the other operand being the value to test.
4.2 - Argument object and testing for undefined
As mentioned in the basic section of this post, the default value for a function argument is undefined if none is given. In addition the default value for an object property is also undefined. The value of undefined will also evaluate to false when converted to a boolean value. So then all of this works as it should when it comes to setting default values for function arguments, and properties of argument objects when making a function.
5 - Using typeof in an expression that tests for undefined can result in a false negative
It would seem that some developers at stack overflow like to use the typeof operator in expressions that test for undefined as it will not throw an error in the event that a variable is not declared.
However if a variable is not undefined that would imply that a variable is defined, but that it not the case. The variable is not even declared, let alone defined.
So then in a way there are three possible states, a variable is not even declared, a variable is declared but undefined, and a variable is declared and is a value other than undefined.
In this above example the value of r can be zero which is the default and will remain so if the variable myVar is both declared and defined. It will have a value of one if the variable is declared but undefined, and a value of negative one if it is undeclared.
6.1 - Using the double not operator with some values including undefined
When the double not operator is used with the undefined value the resulting boolean value is false.
6.2 - Comparing undefined to null
When using the equality operator a value of true is the result. This is because the equality operator converts types to a common type and then compares the result. Both the null and undefined values convert to false, and false equals false, so the result is a true value.
However when the identity operator is used to make a comparison a false value is the result. This is because when the identity value is used type conversion does not occur. The null and undefined values are two different types, so the result is false.
7 - Sparse arrays and the undefined value
7.1 - Basic sparse array example
First off there is starting out with at least one basic example that shows how it is possible to even end up with a sparse array to begin with. One way to end up with one is to just create a new empty array by way of the Array constructor or the bracket syntax. Then use the bracket syntax to set a new element index that is above zero, say at index 9. This would then result in an array with a length of ten, but with only one public key that is set to the numbered key value of 9. Each of the other index values in the array are undefined as no value has been set for the index, thus this would be a kind of sparse array.
7.2 - Creating a sparse array with Array.from
7.3 - Seeing what the public keys are of an array with Object.keys
One way to go about checking out what the deal is with public keys of an array is would be to use a method like the Object.keys method. This method will return an array of public key names for the given object, so then if a sparse array is given to this method the returned result will not end up being an array of numbers from zero to one less of the length.
7.4 - Array map and sparse arrays
7.5 - Using a while loop and making a custom map method
So then there is making a custom array map method using a while loop as one way to go about addressing this issue with sparse arrays and the array map method. By setting a variable to a value of zero, and then using the length of the array as a limit to stop looping, I can then call a given function for each index value, rather than each public key like the array map method does.
9 - Conclusion