Skip to content

Instantly share code, notes, and snippets.

Paige Niedringhaus paigen11

Block or report user

Report or block paigen11

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@paigen11
paigen11 / raw-string-with-expression-interpolation.js
Last active Sep 15, 2019
Another raw string example, but with expression interpolation in the template literal too
View raw-string-with-expression-interpolation.js
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 Sep 15, 2019
Example of the .raw argument in a string
View raw-strings.js
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 Sep 15, 2019
Example using tagged templates
View tagged-template.js
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 Sep 15, 2019
Creating a configurable string via ES6 nested template strings
View nested-template-strings.js
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 Sep 15, 2019
Creating nested template strings in ES5 without template literal assistance.
View es5-nesting-templates.js
var classes = 'header';
function isDesktopSize(){
window.innerWidth > 1400 ? true : false;
};
var navbar = {
isOpen: false
};
@paigen11
paigen11 / template-literals-expression-interpolation.js
Created Sep 15, 2019
Template literal syntax using expression interpolation
View template-literals-expression-interpolation.js
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 Sep 15, 2019
The old way of injecting expression values into strings in JavaScript
View og-expression-interpolation.js
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 Sep 14, 2019
Multi-line string created using template literal syntactic sugar
View template-literal-multi-line-string.js
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 Sep 14, 2019
Multi-line string examples using traditional strings versus template literal back-ticks
View og-multi-line-string.js
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 Sep 14, 2019
Template expression placeholder example
View expression-placeholder.js
function authorize(user, action) {
if (!user.hasPermission(action)) {
throw new Error(
`User ${user.name} is not allowed to ${action}.`
);
}
}
const person = {
name: "Sean"
You can’t perform that action at this time.