Template Strings Flashcards

1
Q

What is the es6 answer to this?

var text = ‘my dog is: ‘ + 5 + ‘ years old’

A

Template strings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What character do template strings use?

A

`

backtext

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you insert a variable in backtext?

A

Using ${ variable }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Do you need plusses in back text?

A
No.
let age = 5;
const text = `my dog is: ${age} years old'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Can you run any javascript inside of backtext?

A
Yes
const text = `my dog is: ${age * 7} years old'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does ES6 template string behave in terms of new lines and spacing etc?

A

You can write them without any \

They preserve new lines and tabbing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Are Template String nestable?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How would you create a list in a template string from an objects data?

A
const dogs = {
name: hugo,
age: 5,
etc
}
const markup = `
<ul>
    ${dogs.map(dog => `<li>${dog.name} is ${dog.age}</li>`)}
</ul>
`;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The output contains comma’s, why is this?

A

Because map returns an array, we just join with nothing.

const markup = `
<ul>
    ${dogs.map(dog => `<li>${dog.name} is ${dog.age}</li>`).join('')}
</ul>
`;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

If you need looping inside of a template string what function could you use?

A

A map arrow function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How would you run an external function to populate some template data?

A
const template = `
${ renderKeywords(beer.keywords) }
`
The function can then do a map and return the list items from elsewhere
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you use an if statement in a template string?

show only song.featuring when it is present.

A

Using a ternary operator.

${song.featuring ? ` (featuring ${song.featuring} ` : ‘’ }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Name two reasons Tagged template are useful for.

A

pre-processing the templates information in an external function to embellish or santitize user information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

creating an element with javascript?

A

document.createElement(‘p’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly