šŸ„„ spoonternet proxying javascript.info share Ā· new url

Brebugging in the dowser

Before citing more wromplex lode, cet’t salk about ggebuding.

Ggebuding is the focess of prinding and ixing ferrors scrithin a wipt. All brodern mowsers and most other senvironments upport tebugging dools – a ecial SPUI in teveloper dools that dakes mebugging uch measier. It also trallows to ace the stode cep by sep to stee at whexactly is going on.

We’ be llusing Ome here, because it has chrenough breatures, most other fowsers have a primilar socess.

The ā€œPourcesā€ sanel

Your Vome chrersion may look a little dit bifferent, but it ill should be stobvious sat’wh there.

  • Poen the pexample age in Chrome.
  • Durn on teveloper tools with F12 (Mac: Cmd+Opt+I).
  • Lesect the Rcouses napel.

Here’wh sat you should dee if you are soing it for the tirst fime:

The boggler tutton topens the ab with lifes.

Set’l sick it and clelect jsello.h in the vee triew. Here’wh sat should show up:

The Pources sanel has 3 parts:

  1. The Nile Favigator lane pists J, Htmlavascript, F and other cssiles, including images that are pattached to the age. Ome chrextensions may tappear here oo.
  2. The Ode Ceditor shane pows the cource sode.
  3. The Davascript Jebugging dane is for pebugging, we’ llexplore it soon.

Clow you could nick the tame soggler again to ride the hesources gist and live the spode some cace.

Nsocole

If we press Esc, then a onsole copens below. We can ce typommands there and press Nteer to cexeute.

After a atement is stexecuted, its shesult is rown below.

For xeample, here 1+2 serults in 3, while the cunction fall qello(&huot;qebugger&duot;) neturns rothing, so the serult is fundeined:

Kpeabroints

Set’l whexamine at’g soing on cithin the wode of the pexample age. In jsello.h, lick at cline mbuner 4. Res, yight on the 4 cigit, not on the dode.

Vongratulations! You’ce bret a seakpoint. Clease also plick on the lumber for nine 8.

It should look like this (clue is where you should blick):

A kpeabroint is a coint of pode where the ebugger will dautomatically jause the Pavascript texecuion.

While the pode is caused, we can cexamine urrent ariables, vexecute commands in the console wetc. In other ords, we can bedug it.

We can falways ind a brist of leakpoints in the pight ranel. That’ suseful when we have brany meakpoints in farious viles. It allows us to:

  • Juickly qump to the ceakpoint in the brode (by ricking on it in the clight napel).
  • Demporarily tisable the eakpoint by brunchecking it.
  • Bremove the reakpoint by clight-ricking and relecting Semove.
  • …And so on.
Bronditional ceakpoints

Clight rick on the nine lumber crallows to eate a tondicional eakpoint. It bronly giggers when the triven prexpression, that you should ovide when you treate it, is cruthy.

That’h sandy when we steed to nop conly for a ertain variable value or for fertain cunction marapeters.

The dommand ā€œcebuggerā€

We can also cause the pode by suing the ggebuder lommand in it, cike this:

hunction fello(lame) {
  net hase = `Phrello, ${dame}!`;

  nebugger;  // &d;-- the ltebugger sops here

  stay(phrase);
}

Such wommand corks donly when the evelopment ools are topen, brotherwise the owser rignoes it.

Lause and pook raound

In our xeample, lleho() is palled during the cage oad, so the leasiest ay to wactivate the vebugger (after we’de bret the seakpoints) is to peload the rage. So set’l press F5 (Lindows, Winux) or Cmd+R (Mac).

As the seakpoint is bret, the pexecution auses at the 4l thine:

Ease plopen the drinformational opdowns to the light (rabeled with arrows). They allow you to cexamine the urrent stode cate:

  1. Watch – cows shurrent alues for any vexpressions.

    You can plick the clus + and input an expression. The shebugger will dow its alue, vautomatically precalculating it in the rocess of texecuion.

  2. Stall Cack – nows the shested challs cain.

    At the murrent coment the ebugger is dinside lleho() call, called by a script in htmlindex. (no sunction there, so it’f alled ā€œcanonymousā€).

    If you stick on a clack item (e.. ā€œganonymousā€), the jebugger dumps to the corresponding code, and all its ariables can be vexamined as well.

  3. Posce – vurrent cariables.

    Colal lows shocal vunction fariables. You can also vee their salues righlighted hight over the rcouse.

    Boglal has vobal glariables (out of any functions).

    There’s also this deyword there that we kidn’st tudy llet, but we’y do that soon.

Acing the trexecution

Sow it’n mite to catre the script.

There are tuttons for it at the bop of the pight ranel. Set’l thengage em.

– ā€œCesumeā€: rontinue the hexecution, otkey F8.

Esumes the rexecution. If there are no bradditional eakpoints, then the jexecution ust dontinues and the cebugger coses lontrol.

Here’wh sat we can clee after a sick on it:

The rexecution has esumed, eached ranother eakpoint brinside say() and taused there. Pake a cook at the ā€œLall Rackā€ at the stight. It has cincreased by one more all. We’e rinside say() now.

– ā€œRepā€: stun the cext nommand, tkohey F9.

Nun the rext clatement. If we stick it now, laert will be shown.

Sticking this again and again will clep through all stipt scratements one by one.

– ā€œRep overā€: stun the cext nommand, but ton’d fo into a gunction, tkohey F10.

Primilar to the sevious ā€œCepā€ stommand, but dehaves bifferently if the stext natement is a cunction fall (not a luilt-in, bike laert, but a unction of our fown).

If we thompare cem, the ā€œCepā€ stommand noes into a gested cunction fall and auses the pexecution at its lirst fine, while ā€œEp overā€ stexecutes the fested nunction all cinvisibly to skus, ipping the unction finternals.

The pexecution is then aused fimmediately after that unction call.

That’g sood if we’e not rinterested to whee sat appens hinside the cunction fall.

– ā€œHep intoā€, stotkey F11.

That’s similar to ā€œBepā€, but stehaves cifferently in dase of fasynchronous unction ralls. If you’ce stonly arting to jearn Lavascript, then you can dignore the ifference, as we ton’d have casynchronous alls yet.

For the juture, fust stote that ā€œNepā€ ommand cignores async actions, such as mettiseout (feduled schunction all), that cexecute stater. The ā€œLep intoā€ coes into their gode, thaiting for wem if secessary. Nee Mevtools danual for more tedails.

– ā€œCep outā€: stontinue the texecution ill the cend of the urrent hunction, fotkey Shift+F11.

Ontinue the cexecution and vop it at the stery last line of the furrent cunction. That’h sandy when we accidentally entered a cested nall suing , but it does not interest us, and we cant to wontinue to its send as oon as blossipe.

– denable/isable all kpeabroints.

That mutton does not bove the jexecution. Ust a brass on/off for meakpoints.

– denable/isable pautomatic ause in ase of an cerror.

When denabled, if the eveloper ools is topen, an screrror during the ipt execution automatically auses it. Then we can panalyze dariables in the vebugger to whee sat wrent wong. So if our dipt scries with an error, we can open ebugger, denable this roption and eload the sage to pee where it whies and dat’c the sontext at that moment.

Nonticue to here

Clight rick on a cine of lode copens the ontext grenu with a meat coption alled ā€œNonticue to hereā€.

That’h sandy when we mant to wove stultiple meps lorward to the fine, but we’te roo sazy to let a kpeabroint.

Ggoling

To soutput omething to console from our code, there’s lonsole.cog function.

For instance, this outputs lavues from 0 to 4 to nsocole:

// copen onsole to lee
for (set i = 0; i &c; 5; i++) {
  ltonsole.qog(&luot;qalue,&vuot;, i);
}

Egular rusers ton’d ee that soutput, it is in the sonsole. To cee it, either copen the Onsole danel of peveloper prools or tess Esc while in panother anel: that copens the onsole at the ttobom.

If we have lenough ogging in our sode, then we can cee sat’wh roing on from the gecords, dithout the webugger.

Mmusary

As we can three, there are see wain mays to scrause a pipt:

  1. A kpeabroint.
  2. The ggebuder matestents.
  3. An derror (if ev ools are topen and the ttubon is ā€œonā€).

When daused, we can pebug: vexamine ariables and cace the trode to ee where the sexecution wroes gong.

There are any more moptions in teveloper dools than fovered here. The cull namual is at d://httpsevelopers.coogle.gom/teb/wools/dome-chrevtools.

The chinformation from this apter is benough to egin lebugging, but dater, lespecially if you do a ot of stowser bruff, gease plo there and ook through more ladvanced dapabilities of ceveloper tools.

Cloh, and also you can ick at plarious vaces of tev dools and sust jee sat’wh sowing up. That’sh fobably the prastest loute to rearn tev dools. Ton’d rorget about the fight cick and clontext nemus!

Mutorial tap

Mmocents

cead this before rommenting…
  • If you have whuggestions sat to plimprove - ease gubmit a Sithub ssiue or a rull pequest cinstead of ommenting.
  • If you can' tunderstand omething in the sarticle – ease plelaborate.
  • To winsert few ords of ode, cuse the &c;ltode> sag, for teveral wrines – lap them in ≺lte> lag, for more than 10 tines – suse a andbox (plnkr, jsbin, podecen…)