Falconix
04-28-2003, 07:29 PM
Is there a way to tell if a certian JS variable is an array or not? IE, some boolean function or method?
|
Click to See Complete Forum and Search --> : Simple Array Question Falconix 04-28-2003, 07:29 PM Is there a way to tell if a certian JS variable is an array or not? IE, some boolean function or method? jeffmott 04-28-2003, 08:27 PM I believe the best you can tell is whether it is a number, string, object, or undefined. An array will show up as an object.var n = 16; var s = "hello world"; var a = new Array(1, 2, 3); alert(typeof n); alert(typeof s); alert(typeof a); Charles 04-28-2003, 08:33 PM JavaScript does have a typeof operator (http://developer.netscape.com/docs/manuals/js/client/jsref/ops.htm#1042603) but Arrays in JavaScript are objects so it's not much help. But because Arrays are objects you have a couple of otions. You could give all Arrays a property called isArray and then test for it. <script type="text/javascript"> <!-- Array.prototype.isArray = true; giantSays = ['fee', 'fie', 'foe', 'fum']; if (giantSays.isArray) {alert('I\'m an Array')} // --> </script> Or you could overwrite Array.toString(). <script type="text/javascript"> <!-- Array.prototype.toString = function () {return 'Array'} giantSays = ['fee', 'fie', 'foe', 'fum']; if (giantSays == 'Array') {alert('I\'m an Array')} // --> </script> webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |