Created by Russ Anderson / @RealRealRuss
Making transactions simple
https://github.com/h5bp/Front-end-Developer-Interview-Questions#js-questions
Ternary means it involves three things, and strings 'em all on one line so it's harder to read.
That sounds hard, and for some strange reason I'm suddenly jonesing for a Dr. Pepper.
I'm probably not alone
my favorite Fizz Buzz answer is walking out of the interview
— Reid Draper (@reiddraper) November 16, 2016
JS event listeners fire not only on a single DOM element but on all its descendants
Inverse of this. Also known as propogation, events on an element will "bubble up" and also fire on all parents
function handleChange(event) {
// track your change
console.log(event.currentTarget.value);
}
var formEls = document.getElementsByTagName('input');
for (var i = 0; i < formEls.length ; i++) {
formEls[i].addEventListener('change', handleChange, false);
}
function handleChange(event) {
// track your change
// this time on e.target
console.log(event.target);
}
var el = document.getElementById('form');
el.addEventListener('change', handleChange);
The latter is the element with the listener attached, the former is the actual element that triggered it
function foo(){
// i pity this code
}();
Immediately invoked function expression
Firefox - SyntaxError: expected expression, got ')'
function foo(){
// i am known as
// a definition or statement
}
var foo = function() {
// i am an expression
// i resolve to a value, even if just "undefined"
};
MDN - An expression is any valid unit of code that resolves to a value.
// this thing here
function foo(){
}();
// is as if you
function foo(){
}
();
// wrote it on separate lines
( function foo(){ } )();
behold the parentheses
Control variable scope
(function foo(){ })();
console.log(foo);
Firefox - ReferenceError: foo is not defined
all variables (var) are declared at the top of any given function scope whether you like it or not (includes function declarations)
1 function hoist(track) {
2 if (track === 'Down With Disease') {
3 var action = 'dance';
4 }
5 else {
6 var action = 'skip':
7 }
8
9 return action;
10 }
line 6 error: 'action' is already defined
1 function hoist(track) {
2 var action;
3 if (track === 'Down With Disease') {
4 action = 'dance';
5 }
6 else {
7 var action = 'skip':
8 }
9
10 return action;
11 }
var declaration "hoisted" to top. Linters don't like this but browsers don't mind.
1 function hoist(track) {
2 if (track === 'Down With Disease') {
3 action = 'dance';
4 }
5 else {
6 action = 'skip':
7 }
8
9 var action;
10
11 return action;
12 }
cue collective moan
Dr. Axel Rauschmayer: "Variables and scoping in ECMAScript 6"
var, const, or let?
const bar = foo + 1;
Firefox - ReferenceError: foo is not defined
you forgot something
let foo;
let bar = {};
let baz = ['jonny', 'phil', 'ed'];
const qux = function() {
// don't return anything
};
console.log(foo, bar.name, baz[4], qux());
// all print undefined
const quux;
// "Missing initializer in const declaration"
const quux = null;
console.log(quux);
// prints null
Undeclared finds you
Except when assigning a value.
// global scope
foo = 5;
console.log(foo);
// prints 5!
// implied global, aka a reason people hate JS
let foo;
console.log(typeof foo); // "undefined" as a string
console.log(typeof bar): // undeclared, but also returns "undefined" AARGH
// preferred
console.log(foo === undefined); // true boolean
const baz = 'undefined';
console.log(baz === undefined); // false. Hooray, I guess.
const foo = null;
console.log(typeof foo); // object let that sink in for a minute
// preferred
console.log(foo === null); // true boolean
let foo; // undefined
const bar = null; // null
// compare the two
console.log(foo == bar); // true boolean
Double equals checks for equality
Triple equals checks for equality and type
Wield accordingly.
var markup = Handlebars.compile(template)(model);
this.$el.html(markup);
this.$el.foundation('reflow');
this would never work
var markup = Handlebars.compile(template)(model);
this.$el.html(markup);
var self = this;
setTimeout(function() {
self.$el.foundation('reflow');
}, 0);
Defers invoking the function until the current call stack has cleared
(whatever that means)
var markup = Handlebars.compile(template)(model);
this.$el.html(markup);
var self = this;
setTimeout(function() {
self.$el.foundation('reflow');
}, 0);
setTimeout takes function from stack and puts in queue
http://russelljanderson.com/nashjsslides