What's wrong with this javascript? Array not defined

49,451

Solution 1

It's not php - you should use

var variable_name = new Array()

or even better

var variable_name = []

Solution 2

That's not how to declare variables as an empty array. You should be using:

var articleHTML = [];

See this previous question for reasoning of using this method instead of new Array()

Solution 3

var articleHTML = new Array();

Solution 4

Note! Javascript IS case sensitive you have to use upper-case A in word Array.

var myarr = new array(); //THIS IS WRONG! and will result in error not defined

So these are the correct ways:

var myarr = new Array(); //THIS IS CORRECT (note the "big" A) :)
var myarr = []; //and this is correct too

Solution 5

It's [] in ECMAScript; this isn't PHP. The interpreter is right - array is not defined, which is why you're getting that.

Share:
49,451
Arlen Beiler
Author by

Arlen Beiler

I like programming, astronomy, history, language, and music. Come unto me, all ye that labor and are heavy laden, and I will give you rest. Take my yoke upon you and learn of me for I am meek and lowly in heart and ye shall find rest unto your soul. For my yoke is easy and my burden is light.

Updated on July 09, 2022

Comments

  • Arlen Beiler
    Arlen Beiler almost 2 years

    What's wrong with this code?

    var divarray = document.getElementById("yui-main").getElementsByTagName("div");
    var articleHTML = array();
    var absHTML;
    var keyHTML;
    var bodyHTML = array();
    var i = 0;
    for ( var j in divarray) {
        if(divarray[i].className == "articleBody"){
      alert("found");
      articleHTML = divarray[i];
      break;
     }
     bodyHTML[i] = '';
     if(articleHTML[i].className == "issueMiniFeature"){continue;}
     if(articleHTML[i].className == "abstract"){absHTML = articleHTML[i]; continue;}
     if(articleHTML[i].className == "journalKeywords"){keyHTML = articleHTML[i]; continue;}
     bodyHTML[i] = articleHTML[i];
     i++;
    }
    

    This is the error I am getting:

    ReferenceError: array is not defined
    

    I am using Google Chrome if it helps any.

  • Alexander Mills
    Alexander Mills almost 9 years
    having fun reading your own code that way - totally difficult to determine where variables are first declared. see python.
  • Alexander Mills
    Alexander Mills almost 9 years
    ruby is also that way for shame