Search code examples
javascriptnode.jsobject-literaltemplate-strings

How to write Variable inside of Variable in Template String literal?


log(`${chalk.magenta('🤖 LAUNCH_COMMAND')} ${chalk.green('npm run: ')} ${chalk.red('${LAUNCH_COMMAND}')}` );

Here is the problem part: ${chalk.red('${LAUNCH_COMMAND}')}

LAUNCH_COMMAND is either 'production' or 'development'. However it's inside of another ${}.

enter image description here


Solution

  • Demo

    Just use the Variable name for nested Variable in Template String literal

    `${chalk.red(LAUNCH_COMMAND)}` // for nested sting literal just use the variable name 
    

    const LAUNCH_COMMAND = 'hi';
    console.log(`${chalk.magenta('🤖  LAUNCH_COMMAND')} ${chalk.green('npm run: ')} ${chalk.red(LAUNCH_COMMAND)}` );
    

    enter image description here