I'm trying to make the property value of an object to be dynamic based on a given condition.
For example, the property value would become Red if the viewport width is <= 1280 , else it would be Yellow .
I'm trying to use the if...else statement but it shows an error: SyntaxError: missing ) after formal parameters.
const firstData = {
if (document.documentElement.clientWidth <= 1280) {
apple: 'Red'
} else {
apple: 'Yellow'
}
};
// Or
const secondData = {
apple: if (document.documentElement.clientWidth <= 1280) {
return "Red"
} else {
return "Yellow"
}
};
console.log(firstData, secondData);