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

146
Visualizações
Change variable from different js

I am new to JS and am currently attempting to make some of my own projects. For one of them, I need to have the same variable in two scripts, I am using NodeJS. For example I want...

var text1
text1 = 'first text'
//in script1.js

to change variable text1 to 'first text' both in script1.js and script2.js and vice versa. My first thought was to store text1 in a txt document and read/write to it, but I think that this won't be so good. Any suggestions on how to do this without saving in a txt document would be appreciated. How to do it without saving in a txt, I'm looking for any solution that works.

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

0

You have to add the keyword export before it

var text1
text1 = 'first text'
export text1

Then on the other file, you have to import it

import { text1 } from './script1.js'

console.log( text1 )

You can export multiple items and make a function to change that variable.

var text1 = 'first text';

const change = (value) => text1 = value;

export { text1 , change }

and then

import { text1 , change } from './script1.js';

console.log( text1 )

change('hello world!')

console.log( text1 )

If you want to export a single variable from a single file you can also make it a default export by adding the default keyword after the export keyword

var text1
text1 = 'first text'
export default text1

Then you don't need to put the curly braces { } in the import statement

import text1 from './script1.js'

console.log( text1 )

Hope you get your answer. 😊😊

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