0

I have search through google with keyword handle javascript magic number but couldn't find one. In c# i would used constant defined number but i don't know what to use in javascript

something like this

public class MagicNumberHelper
{
  const int optionA = 1;
  const int optionB = 2:
  const int optionC = 3
}
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
Sarawut Positwinyu
  • 4,974
  • 15
  • 54
  • 80

2 Answers2

3

Translating that to JS (one of the ways to do it):

var MagicNumberHelper = {
  optionA: 1,
  optionB: 2,
  optionC: 3
}
Blender
  • 289,723
  • 53
  • 439
  • 496
1

is it just a constant number that you can test for later in that case

var MagicNumbers = {
 optionA: 1,
 optionB: 2,
 optionC: 3
};

and then later test for it like this

if(inputNr == MagicNumbers.optionA) {
  // your logic here
}
megakorre
  • 2,213
  • 16
  • 23