Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

template literal types

/** A template literal produces a new string literal type by concatenating
    the contents. When a union is used in the interpolated position, the 
    type is the set of every possible string literal that could be 
    represented by each union member: */

type Taste = "Delicious" | "Spicy";
type Food = "Pizza" | "Meat";
 
type Menu = `${Taste | Food}`;

// Menu will now be one of the following:
// 'DeliciousPizza' | 'DeliciousMeat' | 'SpicyPizza' | 'SpicyMeat'
Comment

template literal syntax

`half of 100 is ${100 / 2}`
Comment

Template literals

If you are gonna copy and paste answers from MDN... you should AT LEAST
check the page hedaer to see what topic you are copying. That way you don't 
post a porno graphic where a 3-D graphic belongs.

`string text`

`string text line 1
 string text line 2`

`string text ${expression} string text`

tag`string text ${expression} string text`
Comment

Template literals (Template strings)

// Untagged, these create strings:
`string text`

`string text line 1
 string text line 2`

`string text ${expression} string text`

// Tagged, this calls the function "tagFunction" with the template as the
// first argument and substitution values as subsequent arguments:
tagFunction`string text ${expression} string text`
Comment

template literal

// Untagged, these create strings:
`string text`

`string text line 1
 string text line 2`

`string text ${expression} string text`

// Re-usable template:
const templateFn = expression => `string text ${expression} string text`;

// Tagged, this calls the function "example" with the template as the
// first argument and substitution values as subsequent arguments:
example`string text ${expression} string text`
Comment

Template Literals

let fourthItem = 'Item 4';
let myHtml = `
  <ol class="item-list">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>${fourthItem}</li>
  </ol>
`;
Comment

Template Literals for Strings

const str1 = 'This is a string';

// cannot use the same quotes
const str2 = 'A "quote" inside a string';  // valid code
const str3 = 'A 'quote' inside a string';  // Error

const str4 = "Another 'quote' inside a string"; // valid code
const str5 = "Another "quote" inside a string"; // Error
Comment

template literals js

var str = "foo";
var myString = `Insert variables inside strings like this: ${str}`;
Comment

Template Literal

const name = 'Jack';
console.log(`Hello ${name}!`); // Hello Jack!
Comment

PREVIOUS NEXT
Code Example
Javascript :: How to Use the replace() String Method in javascript 
Javascript :: javascript strftime 
Javascript :: why we use mongoose 
Javascript :: switch javascript 
Javascript :: angular on back skip routes 
Javascript :: get string length javascript 
Javascript :: mogoosejs 
Javascript :: js promise api 
Javascript :: discord js embed footer 
Javascript :: text input underline react native 
Javascript :: format phone number javascript 
Javascript :: right mouse click js 
Javascript :: v-bind shorthand 
Javascript :: how to pass callback function in javascript 
Javascript :: send params in nested screen 
Javascript :: addAndRemoveClassJquery 
Javascript :: Create a Counter Object or Map in javascript 
Javascript :: create a regex javascript 
Javascript :: scroll js 
Javascript :: Replace symbol if it is preceded and followed by a word character js 
Javascript :: vadd vue router 
Javascript :: change base js 
Javascript :: angular print html 
Javascript :: how to write a funcat in javascript 
Javascript :: sequelize order by nulls last 
Javascript :: scss in react app 
Javascript :: javascript arrays 
Javascript :: datatables buttons do not appear localisation 
Javascript :: avoid compressing imagepicker react native 
Javascript :: how to create instance of class in javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =