Stack Exchange
Include Parentheses () in the Template literals?
How to include Parentheses () in the string when using Template literals?
Currently its output:
Test Yes Test

I expect to output like this with the parentheses:
Test (Yes) Test

Code:
let type = "Yes";
let string = `Test ${type? (type) : ''} Test`;
console.log(string);


You can use template in the template


const type = 'yes';
const string = `Test ${type? `(${type})` : ``} Test`;

console.log(string)




You can wrap the entire placeholder:


let type = 'yes';
let string = `Test (${type? type : ''}) Test`;

console.log(string)



Or if you need to not include it if type is falsy then you can concat them with + or use a nested placeholder:  


let type = 'Yes';
let string = `Test ${type ? `(${type})` : ''} Test`;

console.log(string)



And just for fun you can write a Tagged template to make it more reusable:


function myTag(strings, ...values) {
  let result = '';
  strings.forEach((string, i) => {
    // strings will always be +1 length over values as per the spec
    const value = values[i] ? `(${values[i]})` : ''
    result += string + value;
  });
  return result;
}

const test1 = "Yes";
const result1 = myTag`Test ${test1} Test`;
console.log(result1);

const test2 = null;
const result2 = myTag`Test ${test2} Test`;
console.log(result2);




() should not be included if type is null or undefined.

You need to specify that the parenthesis are part of the string and not just order of operations.
let string = `Test ${type? "(" + type + ")" : ''} Test`;

