106

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?

asanas
  • 3,782
  • 11
  • 43
  • 72

6 Answers6

409

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;
  }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    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? –  Mar 02 '16 at 14:46
  • 4
    @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. – Bergi Mar 02 '16 at 14:48
  • 21
    mind blown. I considered `case` as block. Thanks for this hint! – Andreyco Apr 27 '16 at 21:20
  • 3
    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. – Emrys Myrooin Jul 14 '16 at 09:30
  • Interesting! My code highlighting does not even work in this case! Even they don't know it that it is possible! – Philipp Mochine Jul 29 '18 at 16:49
  • 1
    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. – riv Aug 24 '18 at 13:07
  • @SafalPillai No braces, no block :-) – Bergi Mar 01 '19 at 17:53
  • @Andreyco yep. And JS isn't the only language like this. [C++ is similar](/q/92396/11107541). [and so is Java](/q/10932997/11107541) – starball Jan 30 '23 at 06:26
17

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!

rob2d
  • 964
  • 9
  • 16
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;
}
eltonkamami
  • 5,134
  • 1
  • 22
  • 30
  • 20
    Simply not true. check @bergi answer. – Kuf Jul 18 '16 at 21:58
  • 4
    @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. – toddg Mar 17 '17 at 15:47
  • 3
    See @Bergi answer below – oldo.nicho Aug 24 '17 at 06:52
  • 3
    @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. – Patrick Michaelsen Nov 14 '17 at 22:51
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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Disfigure
  • 720
  • 6
  • 19
3

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);
Zakaria
  • 14,892
  • 22
  • 84
  • 125
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.

sbk201
  • 168
  • 2
  • 8