Skip to content

Instantly share code, notes, and snippets.

View paigen11's full-sized avatar

Paige Niedringhaus paigen11

View GitHub Profile
@paigen11
paigen11 / raw-string-with-expression-interpolation.js
Last active September 15, 2019 18:31
Another raw string example, but with expression interpolation in the template literal too
var str = String.raw`What's up \n${4+7}?`;
console.log(str);
// prints: What's up \n11?
console.log(str.length);
// prints: 15
console.log(str.split('').join(','));
// prints: W,h,a,t,',s, ,u,p, ,\,n,1,1,?
@paigen11
paigen11 / raw-strings.js
Last active September 15, 2019 18:11
Example of the .raw argument in a string
function tag(strings) {
console.log(strings.raw[0]);
}
tag`I'd like this line 1 \n to ignore the newline and be in line with 1
\n even though I should be on line 3 at this point`;
/* prints: I'd like this line 1 \n to ignore the newline and be in line with 1
\n even though I should be on line 3 at this point */
@paigen11
paigen11 / tagged-template.js
Created September 15, 2019 17:22
Example using tagged templates
var guy = 'Paul';
var age = 96;
function myTag(strings, personExp, ageExp) {
var str0 = strings[0]; // "That "
var str1 = strings[1]; // " is a "
var ageStr;
if (ageExp > 99){
ageStr = 'centenarian';
@paigen11
paigen11 / nested-template-strings.js
Last active September 15, 2019 00:55
Creating a configurable string via ES6 nested template strings
function isDesktopSize(){
window.innerWidth > 1400 ? true : false;
}
var navbar = {
isOpen: true
};
classes = `header ${ isDesktopSize() ? '' :
`icon-${navbar.isOpen ? 'collapse' : 'expand'}` }`;
@paigen11
paigen11 / es5-nesting-templates.js
Last active September 15, 2019 00:41
Creating nested template strings in ES5 without template literal assistance.
var classes = 'header';
function isDesktopSize(){
window.innerWidth > 1400 ? true : false;
};
var navbar = {
isOpen: false
};
@paigen11
paigen11 / template-literals-expression-interpolation.js
Created September 15, 2019 00:10
Template literal syntax using expression interpolation
const c = 10;
const d = 5;
console.log(`With the syntactic sugar of ES6 template literals,
doing math in strings like this: ${c + d}
and that: ${2 * c} is a cinch.`);
/* prints: With the syntactic sugar of ES6 template literals,
doing math in strings like this: 15
and that: 20 is a cinch. */
@paigen11
paigen11 / og-expression-interpolation.js
Last active September 15, 2019 00:04
The old way of injecting expression values into strings in JavaScript
const c = 10;
const d = 5;
console.log('It used to be harder to calculate that ' + (c + d)
+ '\n is not the same as ' + (2 * c) + ' in a string.');
/* prints: It used to be harder to calculate that 15
is not the same as 20 in a string. */
@paigen11
paigen11 / template-literal-multi-line-string.js
Created September 14, 2019 23:43
Multi-line string created using template literal syntactic sugar
console.log(`With template literal back-ticks, I can spread strings across
as many lines as I want
with
no
problem.`);
/* prints: With template literal back-ticks, I can spread strings across
as many lines as I want
with
no
@paigen11
paigen11 / og-multi-line-string.js
Last active September 14, 2019 23:42
Multi-line string examples using traditional strings versus template literal back-ticks
console.log("To make a multi-line string this way\n" +
"I have to concatenate two separate strings with a + sign");
/* prints: To make a multi-line string this way
I have to concatenate two separate strings with a + sign */
@paigen11
paigen11 / expression-placeholder.js
Last active September 14, 2019 23:26
Template expression placeholder example
function authorize(user, action) {
if (!user.hasPermission(action)) {
throw new Error(
`User ${user.name} is not allowed to ${action}.`
);
}
}
const person = {
name: "Sean"