115

I have the following code and I get the error 'Duplicate Declaration query_url'.

  switch(condition) {
    case 'complex':
      const query_url = `something`;
      break;
    default:
      const query_url = `something`;
      break;
  }

I understand that query_url is getting declared twice which isn't right. But i don't know how to resolve this. Can someone please help on what should be the correct way to make this work?

2
  • 4
    Where do you need to use query_url? Commented Mar 2, 2016 at 14:45
  • 2
    If you need to use query_url outside the switch statement, you can't use const. Commented Mar 2, 2016 at 14:47

6 Answers 6

424

Try wrapping the cases in blocks:

switch(condition) {
  case 'complex': {
    const query_url = `something`;
    … // do something
    break;
  }
  default: {
    const query_url = `something`;
    … // do something else
    break;
  }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Umm, won't query_url be confined to the block where it is defined, which sort of defeats the whole purpose of assigning to it?
@torazaburo: Yes, it's only to be used in that block. I'm not sure what the actual purpose of the code is, you shouldn't use a switch only to select a single value - an object (or Map) lookup is better suited for that task.
mind blown. I considered case as block. Thanks for this hint!
Thank you ! This is the real solution. Because, in this trivial case, clearly you can define a global variable, but in more complex case, the global variable can be a non-sense in most of switch branches.
In C/C++ you can't even declare variables inside a switch statement without using a block. The main inconvenience is that all editors have a different idea of how to indent braces in this case.
|
18

I personally prefer (and tend to abuse) the following in these sorts of cases:

const query_url = (()=> {
     switch(condition)
           case 'complex': return 'something';
           default:        return 'something-else';
})();

(this requires ES6 or declaring "use-strict" in Node 4.x though)

Update: Alternatively, much more compact depending on if there is any logic there or if it's a simple assignment:

const query_url = { complex: 'something' }[condition] || 'something-else';

Also, of course, depends on the amount of outside-logic embedded in those switch statements!

Comments

13

if you need to redeclare the same variable in each case see @Bergi 's answer bellow

if query_url can have multiple values depending on the switch branch obviously you need a variable ( declare either with var or let ).

const is set once and stays that way.

example usage with let

let query_url = '';
switch(condition) {
  case 'complex':
    query_url = `something`;
    break;
  default:
    query_url = `something`;
    break;
}

4 Comments

Simply not true. check @bergi answer.
@eltonkamami while I don't feel your answer deserves a downvote since it properly addresses the question, I do question why you would declare a variable within a switch case and expect to use it outside of that scope.
See @Bergi answer below
@eltonkamami the answer is sufficient. The question asks how to avoid duplicate definition errors, and this does that. Consider the case where the value query_url passed to another function. In this case, it doesn't matter that query_url is block-scoped because it is simply an intermediate value. You are being unreasonably harsh.
9

You can use {} to scope your switch case.

For your case, you need to return the variable as long as the var exists and is accessible between curly braces:

 switch(condition) {
    case 'complex': {
      const query_url = `something`;
      return query_url;
    }
    default: {
      const query_url = `something`;
      return query_url;
    }
  }

If you won't use return, you must declare a let query_url above your switch statement.

Comments

2

Just put your switch in a function with some return statements :

var condition;
function aSwitch(condition){
switch(condition) {
    case 'complex':
      return 'something';
    default:
      return 'something';
  }
}
const query_url = aSwitch(condition);

3 Comments

if you dont "use strict"; some weird things will occur with your code. each return statement is creating/assinging to a global variable query url.
@antoniskamamis Edited. Thanks for the tip :)
you just have to return the value without an assignment that will either create a global or cause an error
1
const query_url={
  complex:'something complex',
  other:'other thing'
}[condition]

The drawback is,you can't have default with object,you need to have addition check of condition.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.