From a55b575e8089ee6cab7c5c262a7e6db55d0e34d6 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:46:50 +0100 Subject: unslug es: move --- .../global_objects/array/isarray/index.html | 128 +++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 files/es/web/javascript/reference/global_objects/array/isarray/index.html (limited to 'files/es/web/javascript/reference/global_objects/array/isarray') diff --git a/files/es/web/javascript/reference/global_objects/array/isarray/index.html b/files/es/web/javascript/reference/global_objects/array/isarray/index.html new file mode 100644 index 0000000000..b2a115a814 --- /dev/null +++ b/files/es/web/javascript/reference/global_objects/array/isarray/index.html @@ -0,0 +1,128 @@ +--- +title: Array.isArray() +slug: Web/JavaScript/Referencia/Objetos_globales/Array/isArray +tags: + - Array + - ECMAScript5 + - JavaScript + - Referencia + - metodo + - polyfill +translation_of: Web/JavaScript/Reference/Global_Objects/Array/isArray +--- +
{{JSRef}}
+ +

El método Array.isArray() determina si el valor pasado es un {{jsxref("Array")}}.

+ +
Array.isArray([1, 2, 3]);  // true
+Array.isArray({foo: 123}); // false
+Array.isArray('foobar');   // false
+Array.isArray(undefined);  // false
+
+ +

Sintaxis

+ +
Array.isArray(obj)
+ +

Parámetros

+ +
+
obj
+
El objeto a evaluar.
+
+ +

Valor de retorno

+ +

true si el objeto es un {{jsxref("Array")}}; en caso contrario, false.

+ +

Descripción

+ +

Si el objeto es un {{jsxref("Array")}}, devuelve truefalse, en cualquier otro caso.

+ +

Vea el artículo “Determining with absolute accuracy whether or not a JavaScript object is an array” para más detalles.

+ +

Ejemplos

+ +
// las siguientes llamadas devuelven true
+Array.isArray([]);
+Array.isArray([1]);
+Array.isArray(new Array());
+Array.isArray(new Array('a', 'b', 'c', 'd'));
+Array.isArray(new Array(3));
+// Hecho poco conocido: Array.prototype es también un array:
+Array.isArray(Array.prototype);
+
+// todas las siguientes llamadas devuelven false
+Array.isArray();
+Array.isArray({});
+Array.isArray(null);
+Array.isArray(undefined);
+Array.isArray(17);
+Array.isArray('Array');
+Array.isArray(true);
+Array.isArray(false);
+Array.isArray({ __proto__: Array.prototype });
+
+ +

instanceof vs isArray

+ +

Al comprobar una instancia ArrayArray.isArray es más recomendado que instanceof porque funciona a través de iframes.

+ +
var iframe = document.createElement('iframe');
+document.body.appendChild(iframe);
+xArray = window.frames[window.frames.length - 1].Array;
+var arr = new xArray(1,2,3); // [1,2,3]
+
+// Comprobando correctamente un Array
+Array.isArray(arr);  // true
+// Considerado peligroso, porque no funciona a través de iframes
+arr instanceof Array; // false
+
+ +

Polyfill

+ +

Ejecutar el siguiente código antes de cualquier otro código creará un Array.isArray() si no está disponible de forma nativa.

+ +
if (!Array.isArray) {
+  Array.isArray = function(arg) {
+    return Object.prototype.toString.call(arg) === '[object Array]';
+  };
+}
+
+ +

Especificaciones

+ + + + + + + + + + + + + + + + + + + + + + + + +
EspecificaciónEstadoComentario
{{SpecName('ES5.1', '#sec-15.4.3.2', 'Array.isArray')}}{{Spec2('ES5.1')}}Definición inicial. Implementado en JavaScript 1.8.5.
{{SpecName('ES6', '#sec-array.isarray', 'Array.isArray')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-array.isarray', 'Array.isArray')}}{{Spec2('ESDraft')}} 
+ +

Compatibilidad con navegadores

+ +
{{Compat("javascript.builtins.Array.isArray")}}
+ +

Vea también

+ + -- cgit v1.2.3-54-g00ecf