0

I'm a self-/internet-taught Javascript amateur and I'm looking to improve the way I code. My current project has some variable "formatting settings" that are altered throughout the course of runtime, depending on starting conditions and some random generation. I currently push these settings to an array and use Array.include() to search for them when required.

Is there a simpler, more efficient, more "traditional" method of doing this? Initially I used a large quantity of boolean variables but this seemed obtuse and made it very difficult to read. Please let me know if there is any way I can improve this question as well.

Spwack
  • 47
  • 4

1 Answers1

1

You can set them as properties on an object instead.

E.g.

 settings.newlines = true
 settings.includeFoo = false
 settings.useBar = true

To check the value you can then read the property

 if(settings.includeFoo) {...}

Or if the name of the setting is in a variable, you can use this notation:

 var name = 'includeFoo'
 settings[name] = true
 if(settings[name]) {...}
flup
  • 26,937
  • 7
  • 52
  • 74
  • Thanks for your help! Is using objects this way how it is *usually* done? – Spwack Jan 04 '19 at 14:27
  • Using a settings or options object to pass parameters around is quite common. See https://stackoverflow.com/questions/3089561/in-javascript-what-is-an-options-object – flup Jan 04 '19 at 23:19