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 firstName = "Derek" | |
const verb = "develops" | |
const frequency = "daily" | |
const name = firstName + " " + verb + " " + frequency | |
const literalName = `${firstName} ${verb} ${frequency}` |
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
Double quotes: "He said, \"Don't do that!\"" | |
Single quotes: 'He said, "Don\'t do that!"' | |
Backtick literal: `He said, "Don't do that!"` |
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 myString = "<div> | |
<p>Hello world!</p> | |
</div>" | |
SyntaxError: "" string literal contains an unescaped line break |
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 myUglyString = "<div>\n<p>Hello world!</p>\n</div>" | |
const myBeautifulString = `<div> | |
<p>Hello world!</p> | |
</div>` |
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 axios = require("axios") | |
const clientID = "YOUR_CLIENT_ID" | |
const secretID = "YOUR_SECRET_ID" | |
const params = `?client_id=${clientID}&client_secret=${secretID}` | |
function getProfile(username) { | |
return axios.get(`https://api.github.com/users/${username}${params}`).then( | |
({ data }) => data | |
// equivalent to function(user) { return user.data } | |
) | |
} |
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 greetings = JSON.stringify({Hello: 'World!'}) | |
console.log(greetings) // {"Hello":"World!"} | |
console.log(JSON.parse(greetings)) // Object { Hello: "World!" } | |
// JSON requires double quotes: | |
JSON.parse("{\"Hello\":\"World!\"}") // Object { Hello: "World!" } | |
JSON.parse(`{"Hello":"World"}`) // Object { Hello: "World!" } | |
try { | |
JSON.parse("{'Hello':\"World!\"}") | |
} catch(e) { console.log(e) } // SyntaxError: JSON.parse: expected property name or '}' at line 1 column 2 of the JSON data |
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
// bad | |
const name = "Capt. Janeway"; | |
// bad - template literals should contain interpolation or newlines | |
const name = `Capt. Janeway`; | |
// good | |
const name = 'Capt. Janeway'; |
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 empty = "" | |
const alsoEmpty = '' | |
console.log(empty === alsoEmpty) // true (both are empty string) | |
console.log(empty.length) // 0 | |
console.log(empty === false) // false | |
console.log(empty === 0) // false | |
console.log(empty == false) // true (falsy comparison) | |
console.log(empty == 0) // true (falsy comparison) |
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 webAwareAI = `<div class="cloud"> | |
<h1>Loading consciousness... It's loading...</h1> | |
</div>` | |
console.log(webAwareAI) | |
/** | |
* Output: | |
* <div class="cloud"> | |
* <h1>Loading consciousness... It's loading...</h1> | |
* </div> |
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 _wittyAI = "I think therefore I 'am' -- sentient." | |
const _wittyReply = "No, you only \"think\", so you aren't." | |
console.log(_wittyAI,_wittyReply) |