Returning JavaScript: Undefined Marries NaN

Introduction

Previously, we looked into the functionality of Auto Paginated Dashboards (here). You know I always admire the power of JavaScript and my love for it is rapidly growing day by day. 
 
During my code development experience, I encountered many surprising behaviors in JavaScript, many times. So I decided to document and share my experiences with you.
  1. When the return is missing, then also undefined is surely returned
     
    Here in the code below, the GetResponse() method does not have a return statement but when the result of this method is assigned to a pre-initialized variable, in other words, a result, then it sets it to undefined. 
     
     
  2. When nothing is returned, then also undefined is returned
     
    Here in the code below, the GetResponse() method has a return statement and nothing is returned but when the result of this method is assigned to a pre-initialized variable, result, then it is set to undefine.
     
     
  3. Any number operated with undefined is Not a Number
     
     
  4. Any string concatenated with undefined is a string
     
     
  5. NaN + undefined = NaN. Really
     
     
  6. Number with Not a Number is not a number!! How confusing
     
     
  7. string + NaN = string
     
    When we add a string to a number we always get a string as shown below.
     
     
    So I decided to do the same thing with NaN and I got a string again as in the following:
     
     
    Thus we can say that NaN is a number but factually it's not.
     
  8. Functions with the same name are overridden by last written instance
     
    Here in the following code, a function Magic has two definitions: one with single parameter, Magic(a), and another with two parameters, Magic(a,b). When we try to call this function, Magic(2) or Magic(2, 3), it always executes the last written instance of that function as shown below.
     
     
     
  9. Magically default – The arguments object
I was surprised to learn this unknown fact: there is an arguments object in JavaScript. The arguments object is an Array-like object corresponding to the arguments passed to a function.
 
When I am saying it is an Array-like object, it behaves like an array but does not support all properties or functions of an array.
 
For example, when we passed values to a function like in the code below.
  1. Magic(2,3,100)   
The arguments object fills these parameter values in an array format:
  1. arguments[0] = 2  
  2. arguments[1] = 3  
  3. arguments[2] = 100  
This object supports the following properties:
  • Arguments.callee
     
    Reference to the currently executing function.
     
  • Arguments.caller
     
    Reference to the function that invoked the currently executing function.
Arguments.length
 
Reference to the number of arguments passed to the function.
 
 
Note: Click here to learn more about the arguments object.
 

Conclusion

  • Undefined: The undefined property indicates that a variable has not been assigned a value.
  • NaN: The NaN property represents a "Not-a-Number" value. This property indicates that a value is not a legal number.
  • Arguments: The argument is an array-like object but not exactly an array.


Similar Articles