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 geeks: string[] = [ 'GFG' , "Geeks" , 'TypeScript' ];
const num: number = 21; console.log(Array.isArray(geeks)); 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 geeks: string = "Geeksforgeeks" ;
const array: number[] = [1, 2, 3, 4, 5];
console.log(Array.isArray(geeks) ? "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
Recommended Articles