Answers for "how to make a toggle between two strings in js"

0

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];
Posted by: Guest on March-17-2021

Code answers related to "how to make a toggle between two strings in js"

Code answers related to "Javascript"

Browse Popular Code Answers by Language