最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How do I create backtick in backticks? - Stack Overflow

matteradmin5PV0评论

I want to write backtick inside backtick. (es6)

How do you create backtick in backtick ?

const apple = 'apple';
const grape = 'grape';
const money1 = 2000;
const money2 = 1000;


// before
console.log(`${apple} = ${grape === 'grape' ? '(' + money1 + ')' : '(' + money2 + ')'`;

// after  <-- I want
console.log(`${apple} = ${grape === 'grape' ? \`(${money1})\` : \`(${money2})\`}`;

I want to write backtick inside backtick. (es6)

How do you create backtick in backtick ?

const apple = 'apple';
const grape = 'grape';
const money1 = 2000;
const money2 = 1000;


// before
console.log(`${apple} = ${grape === 'grape' ? '(' + money1 + ')' : '(' + money2 + ')'`;

// after  <-- I want
console.log(`${apple} = ${grape === 'grape' ? \`(${money1})\` : \`(${money2})\`}`;
Share Improve this question edited Jun 13, 2019 at 0:35 Jack Bashford 44.2k11 gold badges55 silver badges82 bronze badges asked Jun 13, 2019 at 0:30 KimBenGonKimBenGon 3155 silver badges12 bronze badges 1
  • Your question isn't very clear. What do you want the actual log message to look like? – Barmar Commented Jun 13, 2019 at 0:42
Add a ment  | 

3 Answers 3

Reset to default 4

Remove the backslashes:

const apple = 'apple';
const grape = 'grape';
const money1 = 2000;
const money2 = 1000;

console.log(`${apple} = ${grape === 'grape' ? `(${money1})` : `(${money2})`}`);

If you were asking about printing a literal backpack in a template string, then just use a backslash normally.

console.log(`\``);

To answer your question literaly: "to put a backtick in backticks" sounds like how to put a literal backtick into a string. To do that, escape it.

Escape it with a backslash. See https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Template_literals#Description

`\``

console.log(`\``)

But what you actually are trying to do is not so plex. The JavaScript syntax allows for nesting backticks within ${} without escaping them.

console.log(`Hello ${`world`}, ${`nesting ${`works too`}`}`);

I realize that your example is partially hypothetical, but there's a much simpler approach to your example that avoids the need for nested template literals:

const apple = 'apple';
const grape = 'grape';
const money1 = 2000;
const money2 = 1000;


console.log(`${apple} = (${grape === 'grape' ? money1 : money2})`);

Post a comment

comment list (0)

  1. No comments so far