Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
JavaScript accessing a variable in a different file

So i am trying to access an array in index.js a different file called countries.js. However when i check the console it says that countries is not defined?

index.js

countries.includes('Ethiopia') ? console.log('ETHIOPIA') : countries.push('Ethiopia')

countries.js

 const countries = [
    'Albania',
    'Bolivia',
    'Canada',
    'Denmark',
    'Ethiopia',
    'Finland',
    'Germany',
    'Hungary',
    'Ireland',
    'Japan',
    'Kenya'
  ]

index.html

<body>
    <script src="index.js"></script>
    <script src="countries.js"></script>
    <script src="web_tech.js"></script>
</body>

All the scripts are in the index.html so im stuck as to why i cant access the variable?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

At the time you are executing code in index.js the countries variable does not exist yet. You need to create the variable before using:

<body>
    <script src="countries.js"></script>
    <!-- Now "countries" exist for index.js to use -->

    <script src="index.js"></script>
    <script src="web_tech.js"></script>
</body>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!