Skip to content

Instantly share code, notes, and snippets.

View djD-REK's full-sized avatar
🌞
Full-Stack JavaScript Developer & Doctor of Physical Therapy 🌞

Dr. Derek Austin djD-REK

🌞
Full-Stack JavaScript Developer & Doctor of Physical Therapy 🌞
View GitHub Profile
const firstName = "Derek"
const verb = "develops"
const frequency = "daily"
const name = firstName + " " + verb + " " + frequency
const literalName = `${firstName} ${verb} ${frequency}`
Double quotes: "He said, \"Don't do that!\""
Single quotes: 'He said, "Don\'t do that!"'
Backtick literal: `He said, "Don't do that!"`
const myString = "<div>
<p>Hello world!</p>
</div>"
SyntaxError: "" string literal contains an unescaped line break
const myUglyString = "<div>\n<p>Hello world!</p>\n</div>"
const myBeautifulString = `<div>
<p>Hello world!</p>
</div>`
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 }
)
}
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
// bad
const name = "Capt. Janeway";
// bad - template literals should contain interpolation or newlines
const name = `Capt. Janeway`;
// good
const name = 'Capt. Janeway';
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)
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>
const _wittyAI = "I think therefore I 'am' -- sentient."
const _wittyReply = "No, you only \"think\", so you aren't."
console.log(_wittyAI,_wittyReply)