You can create an Array with all the possible values and use Array.prototype.indexOf() or Array.prototype.includes() to check whether a give value is or is not inside that Array:
const values = [1, 2, 3, 4, 5, 7, 8, 9];
console.log(`[${ values.join(', ') }].indexOf(1) = ${ values.indexOf(1) }`);
console.log(`[${ values.join(', ') }].indexOf(6) = ${ values.indexOf(6) }`);
console.log(`[${ values.join(', ') }].includes(1) = ${ values.includes(1) }`);
console.log(`[${ values.join(', ') }].includes(6) = ${ values.includes(6) }`);
As you can see, indexOf() will return -1 if the value is not inside the Array or the index of its first occurrence otherwise, which might be useful depending on your use case, while includes() only returns true if the value is present or false otherwise, and it's not supported in IE.
So in your case, you would end up with something like this:
const b = 'b';
const c = 'c';
const d = 'd';
const values = [b, c, d];
let target = 'a';
if (!values.includes(target)){
console.log(`${ target } not in [${ values.join(', ') }]`);
}
target = 'b';
if (values.includes('b')){
console.log(`${ target } in [${ values.join(', ') }]`);
}
// You could also do [b, c, d].includes(target) if you don't need to reuse that Array.
This will work with primitive values, so string, number, boolean, null, undefined and symbol, but also with Object references, not to be confused with just Object, as you can see below:
const values = [{ foo: 1 }, { bar: 2 }, { baz: 3 }];
const target = { foo: 1 };
console.log(`values.includes(target) = ${ values.includes(target) }`);
// This will not work because the { foo: 1 } object inside values and the one we are passing to
// includes are not the same, as you can see here:
console.log(`values[0] === target = ${ values[0] === target }`);
// However, if we get a reference to it, then those two references can be compared successfully:
const reference = values[0];
console.log(`values[0] === reference = ${ values[0] === reference }`);
// So now we can also use includes or indexOf:
console.log(`values.includes(reference) = ${ values.includes(reference) }`);
If you want to understand how this works, you could try to implement a function similar to indexOf or includes yourself. Something simple like:
function isValueInArray(arr, value) {
const size = arr.length;
// Iterate over all the indexes in arr:
for (let i = 0; i < size; ++i) {
// Check whether the value at the current index matches the target value:
if (arr[i] === value) {
// If so, we have found value at index i in arr, so we return true and stop searching:
return true;
// Note we could also return the index if we care about the position the value is at.
}
}
// We have checked all the values in arr and none of them matched, so we return false:
return false;
}
const values = [0, 1, 2, 3, 4, 5, 7, 8, 9];
console.log(`isValueInArray(values, 1) = ${ isValueInArray(values, 1) }`);
console.log(`isValueInArray(values, 6) = ${ isValueInArray(values, 6) }`);
However, keep in mind a real implementation is slightly more complicated. For example, you could take a look at this polyfill for includes.
aandbandcare,..if (![b, c, d].includes(a))if all are primitives.stringsare primitives. So will work fine too.