Playing with an Array in TypeScript
Usage of an array in any test automation project using any programming language for handling a set of data is inevitable. Hence I am going to discuss some edge/unique and important cases of an array in TypeScript which will help us to troubleshoot/debug some errors related to data types with respect to arrays.
Here is a sample code snippet:
In the above code, I have declared an array without any type and assigned values of different types. For example I have assigned values of type string, number, empty, null, undefined and space(which is nothing but string type which is having space). I am printing the values of some specific index values.
Now I will execute the code and let us look at the result:
If you observe the array, it has the length 9 and having indexes from 0 to 8.
Now I want to print the value of an index -1 which does not exists then it will print undefined. Basically undefined is a primitive value in TypeScript that represents the absence of a value. It typically occurs when a variable is declared but not assigned a value or when a function doesn't return a value explicitly.
When I print the value if an index 4, it prints nothing. Basically, in the array, it is a space which is a string type. So it prints just a space.
Get Amit Patil Algur’s stories in your inbox
Join Medium for free to get updates from this writer.
When I print the value of an index 5, it prints undefined. Basically, in the array, it is just an empty value. It means we have allocated the memory but did not assigned any value.
When I print the value of an index 6, it prints null because we have assigned a value as null. Null is another primitive value in TypeScript that represents the intentional absence of any object value. It’s often used as a way to explicitly indicate that a variable doesn’t currently reference any object or value and when you want to clear a variable’s value or indicate that it’s intentionally empty.
When I print the value of an index 8, it prints undefined again. Basically, in the array we have assigned value as undefined only. Again as we discussed above, it’s often used to indicate that a variable has been declared but has not yet been assigned a meaningful value.
And the last edge case, when I print the value of an index 9, it prints undefined again as the index 9 doe not exists in an array we declared. Hence it prints the value undefined.
In summary, these above mentioned terms have distinct meanings:
- undefined represents the absence of a value.
- null represents the intentional absence of any object value.
- “Empty” refers to the absence of content and can be context-dependent (e.g., empty arrays, strings, or objects).
- “Space” usually refers to the white space character used to format and separate content.
Thank you for reading until the end. Please consider following the writer and this publication. Visit Stackademic to find out more about how we are democratizing free programming education around the world.











