jeudi 13 décembre 2018

Too long if statement in Javascript

var deviceName = '';

if(deviceName == 'sampleOne'){
   newName = 'One' 
}

if(deviceName == 'sampleTwo'){
   newName = 'Two' 
}

if(deviceName == 'sampletThree'){
   newName = 'Three'
}

I have this simple if statement for Javascript.

How it works?

When a data inputed is sampleOne the output will be One. That's it, very simple right?

Take note that this code is working fine. But my problem is I have so many sample and I think using this kind of If statement is a bad idea because it will be too long. Is there a way to shorten this if statament?

Aucun commentaire:

Enregistrer un commentaire