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

337
Visualizações
In Javascript, is there a way to call a var in another .js file like in Java?

In Java if I wanna call a var from another file in the same folder I can just call the file name and then the var I wanna call. But, I'm looking for a similar thing in Javascript. Please let me know if there is. For Example:

FileName: wordBank.java

int wordCount = 0;

Then in the Other File I can call that wordCount by saying:

wordBank.wordCount = 1; // (assuming wordCount is public)

So is there a way to mimic this code in Javascript?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have to explicitly export and import it like so with es6 wordBank.js:

export const wordCount = 0;

otherFile.js:

import { wordCount } from "./wordBank"
console.log(wordCount)

However, there is no specific way to outright mutate a variable passes between files. You could do something like this:

let wordCount = 0;
export function setWordCount(value) {
    wordCount = value
}

then in the other file:

import { setWordCount } from "./wordBank"

setWordCount(1)
about 4 years ago · Juan Pablo Isaza 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