how to make a toggle between two strings in js
//TOGGLER (between 2 strings)
if (value == 'string0') {
value = 'string1';
} else {
value = 'string0';
}
//alternative ternary format
value = (value == "string0") ? 'string1' : 'string0';
//to do a cycler (like a toggler but with 3 or more strings), first declare an array of the strings
let strings = ['string0', 'string1', 'string2'];
//CYCLER (make sure the variable already has a value as one of the array items)
if (strings.indexOf(value) < strings.length - 1) {
value = strings[strings.indexOf(value) + 1];
} else {
value = strings[0];
}
//alternative ternary format
value = (strings.indexOf(value) < strings.length - 1) ? strings[strings.indexOf(value) + 1] : strings[0];