TypeScript Array.isArray() Method
In TypesScript the isArray() method returns true if the value passed as a parameter is a type of array. If the value is indeed an array the function will return true, otherwise it will return false.
Syntax:
Array.isArray(value: any): boolean
Parameters
- value: The value that will be checked for array type.
Return Value
If the given value is an array then it returns true otherwise returns false.
Example 1: In the below example we are checking if the given variable geek is an array or not.
Javascript
const Beginner: string[] = [ 'GFG' , "Beginner" , 'TypeScript' ]; const num: number = 21; console.log(Array.isArray(Beginner)); console.log(Array.isArray(num)); |
Output:
true
false
Example 2: In the below example we are checking if the given variable geek is an array or not.
Javascript
const Beginner: string = "w3wiki" ; const array: number[] = [1, 2, 3, 4, 5]; console.log(Array.isArray(Beginner) ? "Passed Value is an Array" : "Passed Value is not an Array" ); console.log(Array.isArray(array) ? "Passed Value is an Array" : "Passed Value is not an Array" ); |
Output:
Passed Value is not an Array
Passed Value is an Array
Contact Us