Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!