This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const html = (` | |
| <html> | |
| <body> | |
| <div>Template literals are super cool.</div> | |
| </body> | |
| </html> | |
| `).trim(); | |
| console.log(html); | |
| // <html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const price = 24.99; | |
| console.log("The item costs $" + price + " on the online store."); | |
| // The item costs $24.99 on the online store. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const price = 24.99; | |
| console.log(`The item costs $${price} on the online store.`); | |
| // The item costs $24.99 on the online store. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const price = 24.99; | |
| const discount = 10; | |
| console.log(`The item costs $${(price * (100 - discount) / 100).toFixed(2)} on the online store.`); | |
| // The item costs $22.49 on the online store. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| 'The price of ', | |
| ' units of the item on the online store is $', | |
| '.' | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function pricing(literals, ...replacements) { | |
| // Initialize the final string | |
| let finalString = ''; | |
| for (let i = 0; i < replacements.length; i++) { | |
| // Get the current literal and replacement | |
| const literal = literals[i]; | |
| const replacement = replacements[i]; | |
| // Trim trailing whitespaces from the current literal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const price = 24.99; | |
| const discount = 10; | |
| const quantity = 4; | |
| const totalPrice = quantity * price * (100 - discount) / 100; | |
| // WITHOUT TEMPLATE TAG | |
| console.log(`The price of ${quantity} units of the item on the online store is $${totalPrice}.`); | |
| // The price of 4 units of the item on the online store is $89.964. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // METHOD 1: Short-circuiting | |
| // Using the logical OR (||) operator | |
| function convertToBase(number, base) { | |
| number = parseInt(number) || 0; | |
| base = parseInt(base) || 10; | |
| return number.toString(base); | |
| } | |
| // METHOD 2: Ternary (?:) operator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function convertToBase(number = 0, base = 10) { | |
| return parseInt(number).toString(parseInt(base)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getDefaultNumberBase() { | |
| return 10; | |
| } | |
| function convertToBase(number = 0, base = getDefaultNumberBase()) { | |
| return parseInt(number).toString(parseInt(base)); | |
| } |