Stop using switches the wrong way, use this instead

Introduction

In this example we have a ‘color’ variable and a ‘colorPsychology’ variable, ‘colorPsychology’ will be assigned a string depending of the value of ‘color’, so in this case a switch statement works fine. The problem is that sin…


This content originally appeared on DEV Community and was authored by Bernardo Torres

Introduction

1.png

In this example we have a 'color' variable and a 'colorPsychology' variable, 'colorPsychology' will be assigned a string depending of the value of 'color', so in this case a switch statement works fine. The problem is that since we're just executing one line of code, it ends up being unnecessary long, so we could use a different approach. Lets see

Using an object instead

2.png

A good approach would be using an object where the keys are the names of the color and the values are their respective color psychology

The value assignment of 'colorPsychology' could be a little confusing, but here's what's happening:

  1. the colorPsychology variable is created
  2. JS looks for the the value of the key color in our colorPsyOptions object
  3. since the key is the color variable and this contains a string 'blue', JS will look for the value of the 'blue' key in our object
  4. If the 'color' variable contains a value that doesn't isn't a key in our object, it will assign a string 'unknow'

Don't understand objects yet? check the MDN docs

Using an array when using numbers

In case we're using numbers as keys, we could use a shorter version with an array, like this:

Instead of using this:
3.png

We can do something like this:
4.png

When to use these approaches

When you're just assigning values in the switch statement

Exercises

Replace the following switch statements with the previous approach

You can use playcode

Fruit names translation

5.png

Planet names

7.png

Results

Exercise 1

6.png

Exercise 2

8.png

I hope this was useful to you and consider following me on Twitter as @pallascodes, I'll be posting daily JavaScript content there


This content originally appeared on DEV Community and was authored by Bernardo Torres


Print Share Comment Cite Upload Translate Updates
APA

Bernardo Torres | Sciencx (2021-08-02T19:58:48+00:00) Stop using switches the wrong way, use this instead. Retrieved from https://www.scien.cx/2021/08/02/stop-using-switches-the-wrong-way-use-this-instead/

MLA
" » Stop using switches the wrong way, use this instead." Bernardo Torres | Sciencx - Monday August 2, 2021, https://www.scien.cx/2021/08/02/stop-using-switches-the-wrong-way-use-this-instead/
HARVARD
Bernardo Torres | Sciencx Monday August 2, 2021 » Stop using switches the wrong way, use this instead., viewed ,<https://www.scien.cx/2021/08/02/stop-using-switches-the-wrong-way-use-this-instead/>
VANCOUVER
Bernardo Torres | Sciencx - » Stop using switches the wrong way, use this instead. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/02/stop-using-switches-the-wrong-way-use-this-instead/
CHICAGO
" » Stop using switches the wrong way, use this instead." Bernardo Torres | Sciencx - Accessed . https://www.scien.cx/2021/08/02/stop-using-switches-the-wrong-way-use-this-instead/
IEEE
" » Stop using switches the wrong way, use this instead." Bernardo Torres | Sciencx [Online]. Available: https://www.scien.cx/2021/08/02/stop-using-switches-the-wrong-way-use-this-instead/. [Accessed: ]
rf:citation
» Stop using switches the wrong way, use this instead | Bernardo Torres | Sciencx | https://www.scien.cx/2021/08/02/stop-using-switches-the-wrong-way-use-this-instead/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.