Skip to content

Instantly share code, notes, and snippets.

@nulayuhz
nulayuhz / elseif
Last active August 29, 2015 14:09
There is no elseif in JavaScript
if (a) {
//...a
} else if (b) {
//...b
} else {
//...c
}
//...is actually:
@nulayuhz
nulayuhz / Operator Precedence
Last active August 29, 2015 14:09
Operator Precedence
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
@nulayuhz
nulayuhz / Automatic Semicolons Insertion
Last active August 29, 2015 14:09
Automatic Semicolons Insertion
//https://github.com/getify/You-Dont-Know-JS/blob/master/types%20&%20grammar/ch5.md#automatic-semicolons
"It's important to note that ASI will only take effect in the presence of a new-line (aka line-break).
Semicolons are not inserted in the middle of a line.
Basically, if the JS parser parses a line where a parser error would occur (a missing expected ;),
and it can reasonably insert one, it does so. What's reasonable for insertion?
Only if there's nothing but whitespace and/or comments between the end of some statement and that
line's new-line/line break."
@nulayuhz
nulayuhz / creating DOM elements with ID creates global variables
Last active August 29, 2015 14:09
creating DOM elements with ID creates global variables
https://github.com/getify/You-Dont-Know-JS/blob/master/types%20&%20grammar/apA.md#appendix-a-mixed-environment-javascript
Host Objects
Global DOM Variables
what may be lesser common knowledge is that (because of legacy browser behavior)
creating DOM elements with id attributes creates global variables of those same names.
For example:
@nulayuhz
nulayuhz / Async Console
Last active August 29, 2015 14:09
Async Console
https://github.com/getify/You-Dont-Know-JS/blob/master/async%20&%20performance/ch1.md#async-console
there are some browsers and some conditions that console.log(..)
does not actually immediately output what it's given.
The main reason this may happen is because I/O is a very slow and blocking
part of many programs (not just JS).
So, it may be more performant (from the page/UI perspective) for a browser to
handle console I/O asynchronously in the background.
@nulayuhz
nulayuhz / hoisting
Created December 9, 2014 20:37
hoisting
https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20&%20closures/ch4.md
"When you see var a = 2;, you probably think of that as one statement.
But JavaScript actually thinks of it as two statements: var a; and a = 2;.
The first statement, the declaration, is processed during the compilation phase.
The second statement, the assignment, is left in place for the execution phase."
- declaration comes before the assignment
- function declarations are hoisted, but function expression are not
@nulayuhz
nulayuhz / hoisting (Function declarations before Variable declarations)
Created December 9, 2014 20:40
hoisting (Function declarations before Variable declarations)
https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20&%20closures/ch4.md#functions-first
@nulayuhz
nulayuhz / closure
Created December 9, 2014 21:32
closure
https://github.com/getify/You-Dont-Know-JS/blob/master/scope%20&%20closures/ch5.md
Closure is when a function is able to remember and access its lexical scope even
when that function is executing outside its lexical scope.
function foo() {
var a = 2;
function bar() {
console.log( a ); // 2
@nulayuhz
nulayuhz / closure cont
Created December 9, 2014 22:29
closure cont'
function foo() {
var a = 2;
function bar() {
console.log( a );
}
return bar;
}
@nulayuhz
nulayuhz / Get Array of 10 Years
Last active August 29, 2015 14:11
Get Array of 10 Years
var year = new Date().getFullYear();
var tenYears = [];
for (var i = 0; i < 10; i++) {
tenYears.push(year++);
}
console.log(tenYears);