Skip to content

Commit 780260c

Browse files
author
Genesis Gabiola
committed
End template literals
1 parent 2c07253 commit 780260c

File tree

2 files changed

+88
-20
lines changed

2 files changed

+88
-20
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* ******************************
3+
* Template Literals
4+
* ******************************
5+
**/
6+
7+
// Define Variables
8+
const name = 'John';
9+
const age = 35;
10+
const job = 'Web Developer';
11+
const city = 'New Hampshire';
12+
let html;
13+
14+
/**
15+
* ********************
16+
* Without Template Literals (ES5)
17+
* ********************
18+
**/
19+
html = '<ul><li>Name: ' + name + '</li><li>Age: ' + age + '</li><li>Job: ' + job + '</li><li>City: ' + city + '</li></ul>';
20+
21+
// Alternative syntax
22+
html = '<ul>' +
23+
'<li>Name: ' + name + '</li>' +
24+
'<li>Age: ' + age + '</li>' +
25+
'<li>Job: ' + job + '</li>' +
26+
'<li>City: ' + city + '</li>' +
27+
'</ul>';
28+
29+
/**
30+
* ********************
31+
* With Template Literals (ES6)
32+
* ********************
33+
**/
34+
function hello() {
35+
return 'Hello';
36+
}
37+
38+
html = `
39+
<ul>
40+
<li>Name: ${name}</li>
41+
<li>Age: ${age}</li>
42+
<li>Job: ${job}</li>
43+
<li>City: ${city}</li>
44+
<li>${2 + 2}</li>
45+
<li>${hello()}</li>
46+
<li>${age > 30 ? 'Over 30' : 'Under 30'}</li>
47+
</ul>
48+
`;
49+
50+
document.body.innerHTML = html;

02-fundamentals/README.md

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- [Type Conversion](#type-conversion)
88
- [Numbers & Math Object](#numbers--math-object)
99
- [String Methods & Concatenation](#string-methods--concatenation)
10+
- [Template Literals](#template-literals)
1011

1112
## Intro & File Setup
1213

@@ -43,7 +44,7 @@ You can write and execute JavaScript code within the browser console itself
4344

4445
- `alert('Hello World');` displays an alert box with the text Hello in the browser
4546
- `undefined` means nothing was returned from executed code
46-
- if you type a number, console will return it. If you type `4 + 4` it will return 8
47+
- if you type a number, console will return it, ff you type `4 + 4` it will return 8
4748
- you can select from the DOM by targetting `document` and it will return the HTML document structure
4849
- what you can do within JavaScript file, can do it within the JavaScript console also
4950
- when you reload the browser, you code will be gone, it's usually used for debugging/testing JavaScript code before committing it in a JavaScript file
@@ -64,7 +65,7 @@ comments */
6465

6566
### Console and other functions
6667

67-
- `console.log()` is very useful when testing and debugging codes.
68+
- `console.log()` is very useful when testing and debugging codes
6869
- `console.table();` create a table for objects
6970
- `console.error();` create an error log
7071
- `consoe.warn();` create a warn log
@@ -111,7 +112,7 @@ Three keywords for declaring a variable in JavaScript
111112

112113
- `const` or constant is a variable that cannot be changed or reassigned a new value
113114
- when `const` is used for Arrays, Objects and other types of data that are primitive like strings and numbers (i.e things that can change but cannot be reassigned), we are able to change the values
114-
- cannot reassign the value but we can change/mutate array/object properties.
115+
- cannot reassign the value but we can change/mutate array/object properties
115116
- can add/change the array/objects but cannot reassign/re-declare the array or object
116117

117118
### Conclusion & Recommendation
@@ -122,7 +123,7 @@ use `const` unless:
122123

123124
1. the value is changing,
124125
2. you need to init a variable or
125-
3. you are using the variable in a iterator for a loop.
126+
3. you are using the variable in a iterator for a loop
126127

127128
Using `const` lets your program or anyone else who is looking at your code, know that the variable should not be reassigned. It can be mutated if its an array or object, but it cannot be reassigned as a new primitive value. This will make your code more robust, secure and more readable.
128129

@@ -146,7 +147,7 @@ There are a bunch of different data types but there are _Two Types_ of data type
146147
2. **Number** a number (integers, decimals, floats are all consindered as numbers)
147148
3. **Boolean** true or false (1 or 0)
148149
4. **Null** intentional empty value
149-
5. **Undefined** a variable that has not been assigned a value.
150+
5. **Undefined** a variable that has not been assigned a value
150151
6. **Symbols** (ES6)
151152

152153
### Reference Data Types
@@ -185,7 +186,7 @@ Type conversion is the process of taking a variable and changing its data type.
185186
Two ways to convert data types into strings:
186187

187188
1. `String()` function
188-
2. `.toString()` method
189+
2. `toString()` method
189190

190191
We can convert the following data types to a string:
191192

@@ -272,25 +273,42 @@ Append means to add onto a variable and not replace it. Use (`+=`) append a vari
272273

273274
### String Properties & Methods
274275

275-
Similar to Numbers, String has built-in properties and methods, E.g. `.length`, `.toUpperCase()`;
276-
277-
- `.length` indicates the length of a string
278-
- `.concat()` concatenates the string arguments to the calling string and returns a new string
279-
- `.toUpperCase` returns the calling string value converted to uppercase
280-
- `.toLowerCase()` returns the calling string value converted to lower case
281-
- `.indexOf()` returns the index within the calling String of the first occurrence of the specified value, else will return -1
282-
- `.lastIndexOf()` returns the index within the calling String object of the last occurrence of the specified value, else will return -1
283-
- `.charAt()` returns the character at the specified index in a string
284-
- get last char by `.length - 1`
285-
- `substring()` returns the part of the string between the start and end indexes, or to the end of the string.
276+
Similar to Numbers, String has built-in properties and methods, E.g. `length`, `toUpperCase()`;
277+
278+
- `length` indicates the length of a string
279+
- `concat()` concatenates the string arguments to the calling string and returns a new string
280+
- `toUpperCase` returns the calling string value converted to uppercase
281+
- `toLowerCase()` returns the calling string value converted to lower case
282+
- `indexOf()` returns the index within the calling String of the first occurrence of the specified value, else will return -1
283+
- `lastIndexOf()` returns the index within the calling String object of the last occurrence of the specified value, else will return -1
284+
- `charAt()` returns the character at the specified index in a string
285+
- get last char by `length - 1`
286+
- `substring()` returns the part of the string between the start and end indexes, or to the end of the string
286287
- `slice()` extracts a section of a string and returns it as a new string, without modifying the original string
287-
- slice from left to right, E.g. `.slice(-3)`
288+
- slice from left to right, E.g. `slice(-3)`
288289
- slice from right to left (negative number = return last x letters from the string)
289290
- `split()` splits a String into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split
290291
- `replace()` returns a new string with some or all matches of a pattern replaced by a replacement
291292
- `includes()` determines whether one string may be found within another string, returning `true` or `false`
292293

293294
To distinguish a property from a method:
294295

295-
- properties do not have brackets (`()`) at the end, E.g. `.length`
296-
- methods has brackets (`()`) at the end, E.g. `.toUpperCase()`
296+
- properties do not have brackets (`()`) at the end, E.g. `length`
297+
- methods has brackets (`()`) at the end, E.g. `toUpperCase()`
298+
299+
300+
## Template Literals
301+
302+
**Template literals** are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
303+
304+
Instead of using single (`'`) or double (`"`) quotes we can use back-ticks (`) to indicate a Template Literal. Anything with the back-ticks can be written as normal HTML tags.
305+
306+
To enter a JavaScript variable, we can use the syntax `${variableName}` instead of a bunch of `+`.
307+
308+
We can also add JavaScript:
309+
310+
- Expressions, E.g. `${2 + 2}`
311+
- Functions, E.g. `${hello()}`
312+
- Conditionals, E.g. `${age > 30 ? 'Over 30' : 'Under 30'}`
313+
314+
Template Literals (template strings) method allows for easy concatenation of HTML strings and the syntax is more cleaner and readable.

0 commit comments

Comments
 (0)