Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

104
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda