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
  • 24,324
  • 8
  • 52
  • 74
Suhas R
  • 21
  • 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
  • 24,324
  • 8
  • 52
  • 74
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
  • 912
  • 1
  • 10
  • 17