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

232
Visualizações
How can i get a the value from a variable, which has an another scope in JS?

I am trying to desing the color of a button depending on one input value.

Herefore i am making if clauses to change the const mycolor depending of the input value state1

if(state1=== "good") {const mycolor="green"}
if(state1 === "medium") { const mycolor="yellow"}
if(state1 === "functional") {const mycolor="red"}

console.log(mycolor)

mycolor is not defined, because it is in the scope of the if statement. How can i get access to it?

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

0

Define mycolor outside of the scope.

let mycolor = ''

if (state1 === 'good') 
  mycolor = 'green'
if (state1 === 'medium')
  mycolor = 'yellow'
if (state1 === 'functional')
  mycolor = 'red'

console.log(mycolor)
about 4 years ago · Juan Pablo Isaza Relatório

0

Declare the variable outside of the if statement (using let) and then just mutate it depending on the state.

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