Array Identifier
In JavaScript, distinguishing between different data types is crucial for writing robust and predictable code. One common task is to determine if a given variable holds an array. This is important for operations that specifically require array methods or behavior, preventing errors and ensuring correct logic.
Problem Description
Your task is to create a JavaScript function that takes a single argument and returns true if the argument is an array, and false otherwise.
Requirements:
- The function should accurately identify all JavaScript arrays.
- It should correctly identify non-array values, including primitive types (strings, numbers, booleans, null, undefined, symbols, bigints) and object types (plain objects, functions, dates, etc.).
Expected Behavior:
- Given an array, the function should return
true. - Given any value that is not an array, the function should return
false.
Edge Cases to Consider:
nullandundefined- Primitive data types
- Plain JavaScript objects (
{}) - Functions
- Instances of custom classes
Examples
Example 1:
Input: [1, 2, 3]
Output: true
Explanation: The input is a standard JavaScript array.
Example 2:
Input: "hello"
Output: false
Explanation: The input is a string, not an array.
Example 3:
Input: { a: 1, b: 2 }
Output: false
Explanation: The input is a plain JavaScript object, not an array.
Example 4:
Input: null
Output: false
Explanation: null is a primitive type and not an array.
Constraints
- The function will be called with a single argument.
- The argument can be any valid JavaScript data type.
Notes
Consider the various ways to check for an array in JavaScript. There are built-in methods and properties that can help you achieve this. Think about which method is the most reliable and covers all cases.