'How to write this sentence correct JS [duplicate]

I have 2 variables and i need to make this as a color. For example:

table1.style.cssText = "color: textcolor; background-color: bgcolor; "

where bgcolor and textcolor - variables with color value (red/black for example)



Solution 1:[1]

Use a template literal.

table1.style.cssText = `color: ${textcolor}; background-color: ${bgcolor}; "

Solution 2:[2]

You can inject variables into a string using a template literal string:

const textColor = 'white';
const bgColor = 'black';

table1.style.cssText = `color: ${textColor}; background-color: ${bgColor};`;

Demo:

const msg = 'hello';
const msg2 = 'world';

console.log(`${msg} ${msg2}!!!`);

Solution 3:[3]

You could also assign the values manually:

const textColor = 'white';
const bgColor = 'black';

table1.style.color = textColor;
table1.style.backgroundColor = bgColor;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barmar
Solution 2 mstephen19
Solution 3 code