What is wrong with this javascript? Array not defined

What is 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 get:

 ReferenceError: array is not defined 

I use Google Chrome if this helps.

+4
source share
8 answers

This is not php - you should use

 var variable_name = new Array() 

or even better

 var variable_name = [] 
+20
source

This is not how to declare variables as an empty array. You should use:

 var articleHTML = []; 

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

+7
source

This is [] in ECMAScript; This is not PHP. The interpreter is right - array not , so you get this.

+2
source
 var articleHTML = new Array(); 
+1
source

Instead

 var articleHTML = array(); 

and

 var bodyHTML = array(); 

do

 var articleHTML = []; 

and

 var bodyHTML = []; 
+1
source

First you need to define

 var divarray = new Array(); 
0
source

You also do not need to use var six times, you can do:

 var divarray = document.getElementById("yui-main").getElementsByTagName("div"), articleHTML = [], absHTML = [], keyHTML = [], bodyHTML = [], i = 0; 

It works just as well as your six vars, but it looks a lot nicer.

There are also a number of compelling reasons not to use new in an array instance (other than []; much shorter than the new Array ();)

0
source

Attention! Javascript IS is case sensitive, you must use uppercase A in an array of words.

 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 
0
source

Source: https://habr.com/ru/post/1310151/


All Articles