Git Product home page Git Product logo

javascriptidioms's Introduction

js logo

JavaScript Almanac

Best JavaScript Idioms and some shortcusts

Sites

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

null vs undefined

Hoisting

Function Expression vs Function Declaration

  • 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

[[Prototype]] vs __proto__ vs prototype

The new keyword in javascript

They all does the same thing: Tomato Potato

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

JQuery and Ajax

  • 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(){})

Shorthand equivalent(alias):

$(function(){})

image

JavaScript equivalent of the JQuery code above

image

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

javascriptidioms's People

Contributors

kidusmt avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.