1

I would take up 2 javscript files.

In one file i declare an array of 5 elements.

Can anyone tell me how can i retrieve those array elements in another file.

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
Suhas R
  • 31
  • 5

2 Answers2

1

Suppose one js file name script1.js and another script2.js

script1.js

var ar=["1","2"];

script2.js

console.log(ar);

in your html file place your first script file before 2nd script file

<script src="script1.js"></script>
<script src="script2.js"></script>
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
0

sample.js

var a = new a();
function a()
{
    this.array = [1,2,3];
}

sample2.js

function b()
{
    alert(a.array);
}

sample1.html

<!DOCTYPE html>
<html>
    <head>
        <script src = "sample.js"></script>
        <script src = "sample2.js"></script>
        <script type="text/javascript"> b();</script>
    </head>
    <body>
    </body>
</html>
Newinjava
  • 972
  • 1
  • 12
  • 19