Best JavaScript Idioms and some shortcusts
Instead of this
if("foobar".indexOf("foo") > -1)
Do this
if(~"foobar".indexOf("foo"))
Instead of this
var foo = Math.floor(2.333)
Do this
var foo = ~~2.333
Instead of this
var foo = parseFloat("12.4")
var bar = parseInt("12", 10)
Do this
var foo = +"12.4"
var bar = +"12"
Instead of this
if(isNaN(foo))
Do this
if(foo != foo)
Instead of this
(function(){ ... })()
Do this
!function(){ ... }()
Convert anything to a boolean by prefixing it with !!
var isFoo = !!foo
- best SO explanation
null
: has typeobject
butno value
undefined
: hasno type
andno value
- var hoisting
- Hoisting is JavaScript's default behavior of moving declarations to the top
- Nice article from wsvincent
- JavaScript Hoisting
- DigitalOcean's explanation
- Hoisting precedence:
-
- Variable assignment takes precedence over function declaration
-
- Function declarations take precedence over variable declarations
variable assignment
>function declaration
>variable declaration
-
- To avoid Hoisting:
-
- using
"use strict"
directive on top
- using
-
- using
let
andconst
inplace of var
- using
-
- declaring all variables on top
-
- Declaration: defining a function for later use
hoisted
to the top
function foo(){
console.log("foo here");
}
// called with the name of the function
foo();
- Expression: storing a function in a variable
not hoisted
to the top
var x = function (a, b) {return a * b};
// called using the vairable name
x(4,5);//20
- best stackoverflow summary
- nice article from levelup
- understanding prototype
- awesome site: JS docs
const abc = {
salute: "",
greet: function() {
this.salute = "Hello";
console.log(this.salute); //Hello
const setFrench = function(newSalute) {
this.salute = newSalute;
};
setFrench("Bonjour");
console.log(this.salute); //Bonjour
}
};
setTimeout(abc.greet.bind(abc)); // setTimeout + bind
setTimeout(()=> abc.greet.call(abc)); // setTimeout + call
setTimeout(()=> abc.greet.apply(abc)); // setTimeout + apply
setInterval(abc.greet.bind(abc)); // setInterval + bind
setInterval(()=> abc.greet.call(abc)); // setInterval + call
setInterval(()=> abc.greet.apply(abc)); // setInterval + apply
(abc.greet)(); // function wrap (only)
(abc.greet.bind(abc))(); // bind + function wrap
(() => abc.greet.call(abc))(); // call + function wrap
(() => abc.greet.apply(abc))(); // apply + function wrap
- Document Ready Function: A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ).ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.
$(document).ready(function(){})
$(function(){})
JavaScript equivalent of the JQuery code above
- best stackoverflow explanation
- JSON has to be:
- Key values must be quoted
- Strings must be quoted with " and not '
- You have a more limited range of values (e.g. no functions allowed)
JS versions and release dates: source: greycampus
- ES1 1997
- ES2 1998
- ES3 1999
- ES4 Abandoned
- ES5 2009
- ES6 2015
- ES7 2016
- ES8 2017
- ES9 2018