Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
How to access array from another .js file?

I want to get a (Very large) array from another file so it doesn't take up space in my main js file but I cannot figure it out, I have spent about 2 - 3 days searching online but cannot find an answer that will work. I've tried answers from How to access js array defined in another js file aswell as many other sites but nothing is working.

// the js file with the array I want
wordList = [
    "this",
    "is",
    "an",
    "array"
]
// main js file
const word = wordList[Math.floor(Math.random() * wordList.length)];

Any help is appreciated :) EDIT: This is for a browser game if that helps

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can export the variable from first file using export.

// example.js
const WordList = [
    "this",
    "is",
    "an",
    "array"
]

export { WordList };

Then, import the variable in main.js file using import.

import { WordList } from './example.js'

about 4 years ago · Santiago Trujillo Relatório

0

You should add both the scripts to html. Then, you should make the array global:

window.wordList = [
    "this",
    "is",
    "an",
    "array"
]

Then in the main.js file, you can access the array like this:

const word = window.wordList[Math.floor(Math.random() * wordList.length)];

But make sure, you add the script with the array before the main.js file in the html

about 4 years ago · Santiago Trujillo Relatório

0

You need to export your array and import it in any file you want. Full details of the export/import mechanism can be found in the next Stackoverflow question

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda