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

145
Visualizações
How to access the value of invoked function in switch statemant

Consider the following code:

switch (doSomething()) {
    case "1":
        return 1;
    case "2":
        return 2;
    default:
        console.log(RETURNED_VALUE); // I want here to access the value returned from doSomething
}

I want to print the value returned from doSomething, without storing it first in a variable. Is it possible? (something similar to this)
Thank you

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Good night!

Switch is a conditional structure, like if or ternary operator. It's only a set of instructions, not a object allocated in memory. Because of this, you can't have a "this" feature inside of it. However, if you don't want to store the variable for long time, you can just wrap the switch inside a function, like that:

function switchDebug () {
    let value = doSomething(); 
    switch (value) {
        case "1":
            return 1;
        case "2":
            return 2;
        default:
            console.log(value);
    }
}

This way, the variable will be cleaned after use of function, because it will be a local scope variable of the function.

over 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