Javascript Array detect Arrays

Description

Javascript Array detect Arrays

View in separate window


let myArrayFromConstructor = new Array("hello", "world");

let myArrayFromLiteral = ["hello", "world"];

console.log( typeof myArrayFromConstructor);  

console.log( typeof myArrayFromLiteral);  

function isArray(obj) {//  ww  w  . j av a2s .co  m
  return Object.prototype.toString.apply(obj) === '[object Array]'; 
}

console.log( isArray(myArrayFromLiteral) );

console.log( isArray(myArrayFromConstructor) );
</script>
</body>
</html>



PreviousNext

Related