Skip to content

Commit 21cc2cd

Browse files
authored
Concept exercise: Template string and Ternary (#1428)
1 parent 3c98f37 commit 21cc2cd

File tree

27 files changed

+577
-14
lines changed

27 files changed

+577
-14
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "The conditional ternary operator is used to write a condensed expression that returns one of two alternate values based on some condition.",
3+
"authors": ["pertrai1"],
4+
"contributors": []
5+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# About
2+
3+
The conditional ternary operator is used to write a condensed expression that returns one of two alternate values based on some condition.
4+
It is often referred to as the "ternary operator".
5+
The name stems from the fact that the operator has three operands: `predicate ? consequent-expression : alternative-expression`
6+
It can be used as a replacement for short if-else statements.
7+
8+
Similar to `if` statements, JavaScript will perform implicit type conversion to evaluate the condition.
9+
If the condition is truthy, the operand on the left-hand side of the colon will be returned.
10+
Otherwise the result of the ternary expression is the operand on the right-hand side of the colon.
11+
12+
```javascript
13+
const year = 2020;
14+
15+
year > 2000 ? 'in the past years' : 'a long time ago';
16+
// => 'in the past years'
17+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Introduction
2+
3+
The conditional ternary operator is used to write a condensed expression that returns one of two alternate values based on some condition.
4+
It is often referred to as the "ternary operator".
5+
The name stems from the fact that the operator has three operands: `predicate ? consequent-expression : alternative-expression`
6+
It can be used as a replacement for short if-else statements.
7+
8+
Similar to `if` statements, JavaScript will perform implicit type conversion to evaluate the condition.
9+
If the condition is truthy, the operand on the left-hand side of the colon will be returned.
10+
Otherwise the result of the ternary expression is the operand on the right-hand side of the colon.
11+
12+
```javascript
13+
const year = 2020;
14+
15+
year > 2000 ? 'in the past years' : 'a long time ago';
16+
// => 'in the past years'
17+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Instance_methods",
4+
"description": "MDN: Conditional (ternary) operator"
5+
},
6+
{
7+
"url": "https://javascript.info/ifelse",
8+
"description": "javascript.info: Conditional branching"
9+
}
10+
]

concepts/string-formatting/.meta/config.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

concepts/string-formatting/about.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

concepts/string-formatting/introduction.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

concepts/string-formatting/links.json

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"blurb": "You can create template strings in JavaScript by wrapping text in backticks. They not only allow the text to include new lines and other special characters, you can also embed variables and other expressions.",
3+
"authors": ["pertrai1"],
4+
"contributors": []
5+
}

concepts/template-strings/about.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Introduction
2+
3+
In JavaScript, _template strings_ allows for embedding expressions in strings, also referred to as string interpolation.
4+
This functionality extends the functionality of the built in [`String`][string-reference] global object.
5+
6+
You can create template strings in JavaScript by wrapping text in backticks.
7+
They not only allow the text to include new lines and other special characters, you can also embed variables and other expressions.
8+
9+
```javascript
10+
const num1 = 1;
11+
const num2 = 2;
12+
13+
`Adding ${num1} and ${num2} gives ${num1 + num2}.`;
14+
// => Adding 1 and 2 gives 3.
15+
```
16+
17+
In the example above, backticks - (<code>\`\`</code>) - are used to represent a template string. The`${...}` is the placeholder that is used for substitution.
18+
Any non-string types are _implicitly_ converted to strings.
19+
This topic is covered in the [type conversion][type-conversion-concept] concept.
20+
All types of expressions can be used with template strings.
21+
22+
```javascript
23+
const track = 'JavaScript';
24+
25+
`This track on exercism.io is ${track.toUpperCase()}.`;
26+
// => This track on exercism.io is JAVASCRIPT.
27+
```
28+
29+
When you are needing to have strings formatted on multiple lines:
30+
31+
```javascript
32+
`This is example of using template
33+
strings to accomplish multiple
34+
lines`;
35+
```
36+
37+
With the substitution capabilities that are available, you can also introduce logic into the process to determine what the output string should be.
38+
One way to handle the logic could be using the [ternary operator][ternary-operator].
39+
This gives the same conditional `if/else` functionality in a slightly different format.
40+
41+
To implement logic into template string syntax:
42+
43+
```javascript
44+
const grade = 95;
45+
46+
`You have ${grade > 90 ? 'passed' : 'failed'} the exam.`;
47+
// => You have passed the exam.
48+
```
49+
50+
[string-reference]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
51+
[type-conversion-concept]: /tracks/javascript/concepts/type-conversion
52+
[ternary-operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

0 commit comments

Comments
 (0)