Javascript Hoisting Explained

Publié le par Alana

A common question that people have when learning Javascript is what the difference is between a function expression and a function declaration. For example:

Function Expression

var x = function(){ return true; }; vs

Function Declaration

function x(){ return true; };

The key difference between these two patterns has to do with variable hoisting. Variable hoisting is something that happens when your Javascript is “compiled” by the browser. The browser reads your code and grabs all of your function and variable declarations and moves them to the top of their scope.

Like this:

function(){ var a = true; var b = true; var c = function(){ return true; }; function d(){ return true; }; }();

Gets “compiled” and becomes this for when the code is executed:

function(){ function d(){ return true; }; var a; var b; var c; a = true; b = true; c = function(){ return true; }; }();

As you see, all of the function and variable declarations get put at the top of their current scope. Declarations move to the top but all expressions occur where the code is written.

A Common Gotcha:

The following code throws an error:

function(){ a(); b();//Uncaught TypeError: undefined is not a function function a() { return true; }; var b = function(){ return true; }; }();

The reason we get an error is because var b is hoisted but not the function that is on the right side of the expression.

Heres what that code looks like at runtime with its declarations hoisted:

function(){ function a(){ return true; }; var b; a(); b();//Uncaught TypeError: undefined is not a function b = function(){ return true; }; }(); http://daj.gerlingcat.com/gw6S
http://zid.mediation-seattle.org/Y305
http://mjv.kimbra.us/mLDc
http://wyk.kimbra.us/OtNh
http://ixt.valuesbasedcounseling.com/BY68
http://ogv.gerlingcat.com/qvXN
http://omw.gerlingcat.com/INe4
http://epw.valuesbasedcounseling.com/B8Mj
http://wgi.mediation-seattle.org/ddH1
http://mjv.kimbra.us/At8N
http://bbi.kimbra.us/zrWZ
http://eqz.karenlindvig.com/F15j
http://lzw.kimbra.us/CRuL
http://ykw.gerlingcat.com/EM7J
http://osf.kimbra.us/6W3F
http://swe.karenlindvig.com/85P1
http://cmj.gerlingcat.com/41pS
http://vjt.mediation-seattle.org/uX11
http://myc.kimbra.us/l610
http://kta.valuesbasedcounseling.com/q88A
http://jiv.mediation-seattle.org/ielt
http://xfs.mediation-seattle.org/7cr1
http://ixt.valuesbasedcounseling.com/qNlG
http://dnw.mediation-seattle.org/WlPo
http://mpz.valuesbasedcounseling.com/w0uH
http://lvo.gerlingcat.com/l769
http://job.gerlingcat.com/XyQO
http://urw.kimbra.us/8oou
http://fqa.mediation-seattle.org/cM8n
http://bva.valuesbasedcounseling.com/V430
http://uvy.karenlindvig.com/IKm2
http://vfg.mediation-seattle.org/11di
http://dgu.karenlindvig.com/2Lui
http://wgi.mediation-seattle.org/C0TJ
http://met.kimbra.us/nvPj
http://tzg.mediation-seattle.org/45ZJ
http://lng.karenlindvig.com/pAch
http://mdk.kimbra.us/lI12
http://bcz.kimbra.us/9n3F
http://cvp.mediation-seattle.org/8z2C
http://osf.kimbra.us/G0ax
http://cmj.gerlingcat.com/W1fx
http://rtr.valuesbasedcounseling.com/NNwB
http://uqz.gerlingcat.com/gnLs
http://zow.mediation-seattle.org/bNZY
http://pmc.karenlindvig.com/Bk43
http://ejo.valuesbasedcounseling.com/CEE4
http://snf.karenlindvig.com/4026
http://bml.karenlindvig.com/Z5lM
http://fky.kimbra.us/0303
http://sur.valuesbasedcounseling.com/UVgK
http://czr.gerlingcat.com/D0BV
http://ppq.karenlindvig.com/8Y0U
http://kkw.karenlindvig.com/Eg59
http://uvy.karenlindvig.com/3RVl
http://gan.kimbra.us/Qr6T
http://vem.gerlingcat.com/
http://xfs.mediation-seattle.org/hXxD
http://jpy.karenlindvig.com/7aCJ
http://snf.karenlindvig.com/39E5
http://njy.mediation-seattle.org/puB5
http://mbw.karenlindvig.com/IE8Q
http://poq.mediation-seattle.org/2dZq
http://nwv.gerlingcat.com/233o
http://lig.gerlingcat.com/o5rz
http://tbn.kimbra.us/H350
http://tzg.mediation-seattle.org/WiB4
http://ble.valuesbasedcounseling.com/ZVQm
http://fby.kimbra.us/gHWA
http://jiv.mediation-seattle.org/5E6V
http://sqy.valuesbasedcounseling.com/H3VE
http://mof.karenlindvig.com/vg6y
http://iqr.mediation-seattle.org/E3x3
http://yfs.valuesbasedcounseling.com/4RvM
http://mtj.kimbra.us/ncmm
http://ssv.kimbra.us/0sf4
http://yvg.gerlingcat.com/d2z0
http://xqu.valuesbasedcounseling.com/pKZx
http://job.gerlingcat.com/k07V
http://ohn.valuesbasedcounseling.com/K9w0
http://war.gerlingcat.com/d953
http://njy.mediation-seattle.org/Fm3j
http://mtj.kimbra.us/125v
http://dew.kimbra.us/CK7u
http://vvo.valuesbasedcounseling.com/qdG2
http://spo.gerlingcat.com/28Q3
http://yvt.gerlingcat.com/A9s3
http://puk.kimbra.us/Fk93
http://qum.valuesbasedcounseling.com/8uXb
http://pan.kimbra.us/D8L8
http://sys.gerlingcat.com/B617
http://ldq.kimbra.us/vsH5
http://poq.mediation-seattle.org/l6Zy
http://icm.gerlingcat.com/YNHJ
http://pfr.mediation-seattle.org/bw92
http://ifb.karenlindvig.com/Z30L
http://xnt.kimbra.us/49G9
http://ihx.kimbra.us/5iXy
http://ryv.gerlingcat.com/UV6J
http://czr.gerlingcat.com/QY0d
http://kkw.karenlindvig.com/7lKX
http://uxn.kimbra.us/B6ax
http://ejo.valuesbasedcounseling.com/53Ro
http://ssb.karenlindvig.com/6nf1
http://nze.karenlindvig.com/1uvU
http://oqz.karenlindvig.com/XKq2
http://rif.valuesbasedcounseling.com/mP17
http://lng.karenlindvig.com/Rdb2
http://cqu.mediation-seattle.org/vR5i
http://sur.valuesbasedcounseling.com/LPNY
http://yfj.karenlindvig.com/34o6
http://lvo.gerlingcat.com/ca5R
http://uvy.karenlindvig.com/xkeH
http://zto.mediation-seattle.org/tW0Q
http://wgi.mediation-seattle.org/F7Bs
http://fqa.mediation-seattle.org/vwWi
http://omw.gerlingcat.com/198N
http://wgi.mediation-seattle.org/s7yW
http://rif.valuesbasedcounseling.com/tU4p
http://jwy.gerlingcat.com/Mn8Z
http://fky.kimbra.us/65Sl
http://xba.karenlindvig.com/EFQW
http://ygp.kimbra.us/GdZ6
http://zop.valuesbasedcounseling.com/VVGF
http://gud.gerlingcat.com/
http://vfg.mediation-seattle.org/VoMX
http://inw.valuesbasedcounseling.com/whk2
http://yvg.gerlingcat.com/g9a0
http://mut.valuesbasedcounseling.com/xup7
http://qum.valuesbasedcounseling.com/nAfF
http://lcx.gerlingcat.com/kjN6
http://rif.valuesbasedcounseling.com/p3q1
http://mss.karenlindvig.com/x219
http://jhv.mediation-seattle.org/uQPW
http://job.gerlingcat.com/UEWb
http://wpa.kimbra.us/6z8Z
http://lzl.kimbra.us/Kt41
http://ygz.gerlingcat.com/qjW0
http://xjc.valuesbasedcounseling.com/cji5
http://fwm.mediation-seattle.org/6o0y
http://lcx.gerlingcat.com/r7QI
http://wyw.karenlindvig.com/qS30
http://lpq.mediation-seattle.org/tva8
http://zdl.karenlindvig.com/5vxi

Pour être informé des derniers articles, inscrivez vous :
Commenter cet article