Sunteți pe pagina 1din 81

JavaScript Programming

A Complete Guide For Beginners To Master And


Become An Expert In JavaScript Programming
Language

By Brian Draper
Cright 2017- All rights reserved
This dumnt i grd twrd providing exact nd rlibl infrmtin in rgrd to
th ti nd iu vrd. Th publication i sold with th id tht the publisher i
not rquird t render unting, officially rmittd, or thrwi, qulifid services.
If dvi i necessary, lgl r professional, a practiced individul in the profession
should be ordered.

- Frm a Dlrtin of Prinil whih was td nd rvd equally by a


Cmmitt of th Amrin Bar Association and a Cmmitt of Publishers nd
Aitin.

In no w i it lgl to rrdu, duplicate, r trnmit n part f thi dumnt in


ithr ltrni mn r in printed frmt. Rrding f thi ublitin i tritl
rhibitd nd any trg f thi dumnt is nt llwd unless with written
permission frm th ublihr. All rights rrvd.

Th infrmtin rvidd hrin i stated t b truthful nd nitnt, in that n


libilit, in trm f inattention r thrwi, b n ug r bu f n lii,
processes, r directions ntind within is th litr nd utter responsibility f th
riint rdr. Undr n circumstances will n lgl responsibility or blame b hld
gint th ublihr fr n rrtin, dmg, or mntr loss du to the
information hrin, ithr dirtl or indirectly.

Rtiv uthr wn ll right not hld b th ublihr.

The infrmtin herein i ffrd fr infrmtinl ur ll, nd i universal as


. Th rnttin of the information is withut contract or n t of guarantee
urn.

Th trdmrk tht r ud r withut n nnt, nd th ublitin of the


trdmrk i withut rmiin or backing b th trdmrk owner. All trdmrk nd
brnd within thi book r for lrifing purposes nl nd r th wnd by th
wnr thmlv, nt ffilitd with this dumnt.

Dilimr and Terms of U: The Author nd Publisher has strived to be as urt


nd complete ibl in th rtin f thi bk, ntwithtnding th ft that h
d nt wrrnt r represent t n time tht th ntnt within are accurate du t th
rapidly changing ntur of the Intrnt. While ll ttmt hv been made to vrif
information provided in thi ublitin, th Authr nd Publisher um no
responsibility fr errors, miin, r contrary intrrttin f th subject matter
hrin.
Any perceived slights of ifi rn, l, r rgniztin are unintntinl. In
rtil dvi bk, like nthing l in lif, thr are n gurnt of rult.
Rdr r utind t rely n their wn judgment but thir individul
circumstances nd act rdingl.

Thi bk i not intndd fr u a ur f legal, mdil, buin, unting or


financial dvi. All rdr r dvid to seek rvi of mtnt rfinl in
the lgl, mdil, buin, unting, nd finance fields.
Table of Contents
1. Introduction to JavaScript Programming
2. Can JavaScript handle Large Scale Enterprise Appilcations?Four Powerful
Features of JavaScript Programming Language
3. How to get rid of the JavaScript Warning on Internet Explorer
4. Errors about JavaScript
5. Easy tips you can use to remove a JavaScript Error
6. Adding JavaScipt in Dreamweaver 8
7. How good is JavaScript for building a large scale web application
8. Why would companies hire JavaScript Developers
9. JavaScript Where to
10. JavaScript Output
11. JavaScript Syntax
12. JavScript Operators
13. JavaScript Statements
14. JavaScript Keywords
15. JavaScript Comments
16. JavaScript Variables
17. JavaScript Operators
18. How to use JavaScript tp get user entry and use the entry in a calculation
19. Conclusion
Introduction t Jvrit Programming
Jvrit i a lint-id rgrmming lngug wh ring engine is mbddd
in web brwr like Internet Explorer, Netscape, Firfx, t. Thi nbl the
processing ngin t read nd interpret wb g that contain the jvrit d whn
brwing.

Jvrit first md an rn in 1995, although by that tim it was knwn


Livrit. Thi name was later hngd to jvrit.

Javascript was invntd to add mr exciting ftur to HTML g. HTML n it' n


i rtt boring and it nnt do muh bid dil a wbg. Jvrit
rgrmming, n the thr hnd, n rvid a ht f xiting features lik rfrm
lultin, llw th user t interact with wbg, rfrm form validations,
mniult elements n a wbg, rvid xllnt viul effects, nbl dnmi
functions nd muh more.

All of this rvid the user to th site with an nhnd ur xrin. Bu


jvrit is a lint-id lngug, ll of th features r vilbl in rl tim
without th browser having t rfr t the server all the time. Thi prevents tim delays
nd increases ur tiftin.

JvSrit i bill a programing lngug rtd b the mn Nt. Th


language h gind uit a lt f popularity in th recent mnth it llw th web
dvlr to mdif the brwr tin well th ntnt of th it in uh a
manner which is nt t ll possible using th trditinl HTML tl or even CSS. Use
f JvSrit n a wb g mwr the developer t ntrl the way hi g rt,
and bhv to specific actions frm th user, int the document elements displayed
n th g, validate the form tin before they r ubmittd, hk th ntiv
browser dtil, t ki for specific rtin, add date and tim t th g, and
vn mbd iml mini gm in the wbit. Th mlt lngug i iml t learn
nd undrtnd n u hv the basic knwldg f OOPS nt nd HTML.

Sftwr nd Knowledge Ndd

Bfr starting u with th new language, u mut have a basic undrtnding of HTML
head and bd r, and fw iml ttmnt ud t dd img, inrt hyperlinks,
make rgrh in the hrtxt lngug. Th ftwr which u n use fr trting
th JvSrit tour i bi text ditr. Windows OS ur m go fr Notepad or
Notepad++ nd M Ur can u TxtEdit or TextWrangler. Th d would nd a
wb brwr (rfrbl Mozilla Firefox or Google Chrm) for successful
miltin and running.
Getting Strtd

A Simple Program t rint a custom message in JvSrit lk something lik:

<html>

<head>

<title>JavaScript example</title>

<rit lngug="JvSrit">

<!--

dumnt.writ("M Firt JavaScript Program!");

//-->

</rit>

</hd>

<body> Hello World! </bd>

</html>
Th u of "<!-"and "//->" in the bv d ensures that th d is nt displayed b
older versions f brwr which lk JvSrit urt. This nur tht th g
d nt mlfuntin n unurtd brwr.

Jut lik n other HTML fil, you just need t copy t the above code in ur
Notepad dumnt, and thn save it with an extension of ".html". Aftr th v i
mlt, iml right lik n the file, and lt option "Open With". From hr,
h a web brwr (Mozilla Firfx if vilbl). Th d wuld xut on it
wn, and rult would b dild in the brwr windw.
Points to Rmmbr

JvSrit d are mostly placed in th "hd" rtin of the html but you m even
use thm under "bd" r.

A JvSrit d nrmll trt with tg "<rit language="JavaScript">" and nd


with "</rit>".

A JavaScript will lw b mbddd int HTML code, and it can nvr tnd alone.
Alrt Message Bx

If u wnt t dd a utm lrt mg bx to your it, thn this n be tblihd


vr easily using JvSrit. Th code fr dding a utm alert bx is follows.

<body>

<script>

window.alert("This i JvSrit Alert Bx!")

</script>

</body>
Similrl you m dd thr lrt like:

1. Confirm Action

Windw. Confirm ("Are you sure u wnt t exit from thi it?")

2. Prmt Action

Windw.rmt("Ur nm fild nnt b lft blank.")

Let's tk a lr look t the grt thing u n d with jvrit rgrmming:


Prfrm Calculations

Javascript n be ud to perform iml to dvnd mthmtil calculations. This i


ill uful if u wnt ur ur to interact with your site b inutting m
infrmtin tht requires some sort f calculation nd displaying the relevant ntnt
bd on that result. Fr xml, jvrit can lult a loan rmnt nd dil
the rrit result if a ur ntr a ruird ln munt n th it. Furthrmr,
jvrit n also tell dt and time, tim a certain activity nd perform intricate date
nd tim calculations.

User Intrtin

Th javascript n the webpage can tk intrtin frm a ur and perform a rtin


funtin b w vnt. Sm f the most popular vnt r:

nblur
onchange
nlik
ondblclick
onfocus
nkdwn
nkr
nku
onload
onmousedown
nmumv
nmuut
nmuvr
nmuu
nlt
onsubmit

Prfrm Frm Vlidtin


Jvrit n vlidt nd hk ur inut t dtrmin whthr it nfrm t a
rtin ruirmnt. For xml, u n hk if a fild i lft empty, if n email
ddr is in the rr frmt, if a zi d d i in th rr format, t. In mt
cases, if n error i md by your it viitr, an lrt can dil th rrit rrr
mg to him or hr.
Manipulate Elements On A Wbg

Jvrit llw a ur t change content r tl f a webpage un a ur


intrtin b way of the frmntind vnt. For example, hovering over a certain
txt n make an img r, txt on a submit button can change n ubmit, t.

Prvid Viul Efft

Javascript has a ht f w t rvid viul effects t a webpage tht thr server-


side rgrmming lngug cannot. A til xml of thi might be some fancy
img rllvr buttn.

Enables Dnmi Functions

Javascript, tgthr with AJAX (asynchronous JvSrit nd XML), n b ud to


create dnmi litin. On xml f thi i t ld dnmi ntnt nt a
wbg without refreshing th page.
Cn JavaScript Handle Lrg Sl Entrri
Alitin?

Arding to the ltt ug ttiti posted on W3Techs.com, over 91% of ll


wbit u JavaScript as a lint-id rgrmming language. Thr are also a
number of programmers wh rfr writing bth lint-id and rvr-id d in
JavaScript. Hwvr, th dvlr have t rely on frmwrk lik Node.js t use
JavaScript fr rvr-id development. JavaScript n effectively handle lrg-l
and complex ntrri litin.

Why JvSrit can handle Complex Enterprise Alitin Efftivl?

Simplify Cmitin f Cmlx Entrri Alitin

Th rgrmmr can easily du dvlmnt f a complex ntrri litin


b imlifing its mitin. JvSrit librri mk it ir fr rgrmmr t
imlif th mitin f mlx business applications b rting hdw DOM
bundri. Th Shadow DOM mk wb browsers t gnrt nd deliver documents
using common HTML tg lik div, lt nd inut. In dditin t decoupling th
mnnt f individul JvSrit frmwrk, th hdw DOM will further
facilitate ml mmunitin btwn th mnnt. Thu, JavaScript will help
rgrmmr t mintin and mng fftivl b imlifing their mitin.

Rniv Wb Dign

Mn urv hv highlightd tht a lrg rntg f people nowadays access


wbit on thir mobile devices. Likewise, a grdul inr is bing noted in the
number of organizations imlmnting bring your own dvi (BYOD) policies. S th
buin nd t nur that the enterprise litin delivers ulit user
xrin r different dvi. A a mdrn wbit dign thniu, responsive
wb design hl buin to mk thir wb-bd enterprise deliver ulit user
experience across a vrit f dvi. But rniv web design mhiz n ug
f n thnlgi like HTML5, JvSrit nd CSS3.

Imrhi Alitin Dvlmnt

Mn buin nowadays t for imrhi JavaScript dvlmnt t create wb


litin tht dlivr richer user experience r many devices. Th rgrmmr
also find it ir t mng, update nd mintin th isomorphic JvSrit
applications. Th dvlmnt mdl ruir rgrmmr to writ bth lint-id
nd rvr-id d in JvSrit. As th ntir d b i dvld uing a single
programming lngug, it becomes ir for developers to mng and udt withut
utting any extra tim nd ffrt.
Trnilr to Simlif JvSrit Prgrmming

JavaScript w riginll dignd a lint-id riting language of wb brwr.


S it lk m f th advanced features provided b mdrn rgrmming lngug
like Java or C#. But th dvlr still have option to u a numbr f trnilr to
simplify JvSrit rgrmming. Th users l hv tin to choose frm a numbr
f trnilr including CffSrit, TypeScript, DukeScript and Vdin. These tools
make it ir for rgrmmr to dd functionality t th ntrri litin by
mking th wrkflw complex. These trnilr furthr mk JavaScript effective in
handling a vrit f mdrn ntrri litin.

Avilbilit f Mn JvSrit Frmwrk

Sm f these JavaScript frmwrk are rbut nd full-fturd, whr thr are


iml. Th programmers n u lrg frmwrk lik Angulr nd Ember to dd
funtinlit t th ntrri application rapidly. At th m tim, they also hv
option t use lightwight librri lik React t accomplish a ifi tk ffiintl.
Al, they can u ilizd tl lik Nd.j t u JavaScript as a rvr-id
rgrmming lngug. At the m tim, they n vil tools lik NPM t install nd
mng th JvSrit libraries used by the ntrri litin mthl and
fftivl.

On the whl, a buin has option to u JvSrit fr dvling bth frntnd nd


bknd f th enterprise applications. It can furthr urtil the dvlmnt tim nd
t ignifintl b dling full-tk dvlr t writ bth lint-id and
rvr-id code in JvSrit. Hwvr, the programmers will need a vrit of
JvSrit librri, tools nd frmwrk to rt more ffiintl.
Fur Pwrful Ftur f JvSrit
Programming Lngug

In th community of wb dvlr nd urfr, JvSrit i highly popular as client


id riting language fr web brwr. In n wb litin, JavaScript n b
used t implement iml ftur like rllvr f img wll to mk
nhrnu rut t server using ajax. Few years bk whn flh was nt
ulr, JvSrit w widely ud t add butiful fft t wbg and is till
bing ud fr the m purpose. Lt' tk a lk t m f th ftur of this
lngug.

1) Browser support: To flh content, you need to install flh lugin in ur


brwr. But to use JavaScript, you don't hv to u any lugin at ll. This is because
ll brwr have td JvSrit a scripting language for thm nd provides
integrated urt fr it. All you need t d i t hndl m of th tk tht r
dependent n DOM (Document Objt Mdl) of diffrnt brwr rrl.

2) Can b ud on lint id wll n server id: As JavaScript h to


Dumnt bjt model f browser, u n tull change the trutur f wb g
at runtime. Du t thi, JavaScript n b used to dd diffrnt fft to wbg. On
th other hand, JvSrit uld be ud on th rvr id wll. Fr example, in
Alfr whih i a popular n ur ntrri content mngmnt system,
JvSrit i ud in creating wbrit. Thi makes dding custom tasks t alfresco
uit simple.

3) Funtinl programming lngug: In JvSrit, funtin uld b ignd to


vribl jut lik n thr dt t. Nt nl tht, but a funtin n t nthr
funtin a rmtr and n l rturn a funtin. Yu n hv funtin with no
name as well. Clrl, this giv you th bilit to d in funtinl rgrmming tl.
4) Support fr bjt: JvSrit i n bjt rintd lngug. However, th w
JvSrit hndl bjt and inhritn i bit diffrnt frm conventional bjt
rintd rgrmming languages like Jv. Due to this, JvSrit urt mt f the
bjt oriented nt whil bing iml t learn nd u.

These r some f th features tht give JvSrit n bilit t hndl iml wll
mlx tasks. Du t thi, JvSrit h rmind th mt popular rgrmming
language fr a long tim. It is l a good lngug for l wh wnt t learn
mutr rgrmming it urt bjt rintd as well funtin nt and t
use it, u jut need a brwr nd a text ditr.
How to Gt Rid of the JavaScript Wrning on
Intrnt Explorer

JvSrit i th tndrd rgrmming language for wb g n th intrnt.


JvSrit rrr r bug in th rgrmming d fr a g.

Bk t th dwn f th intrnt, wb pages wr limitd to uing th Hrtxt Mrku


Language (HTML). HTML is a tndrd ud by all browsers t display web g.
HTML hndl things lik font styles, links, nd itur, whih w nugh t firt, but
people wntd t d mr with wb g.

Nt ud a iml rgrmming lngug developed fr JvSrit tht even


non-programmers uld enhance thir websites with funtinlit tht i mr
hititd. The language itlf has bm mr sophisticated and i now a tndrd
whih, like HTML, is supported b ll wb brwr.

JvSrit w nt designed to run xtrml mlx programs; it was designed fr


iml nhnmnt t wb g. A th intrnt has vlvd, th programming
behind wb g has bm mr complex, ftn trining th limits f JvSrit.
T llvit th rblm m f the programming h bn offloaded to servers rather
than tring t dwnld it all nt ur computer.

JvSrit, whih run lll, i urrntl ud hvil by wbit t drive their user
intrf. Althugh mn modern wbit mmunit with rvr t get thir dt,
tht communication n b lw r vn fil altogether. It is imrtnt fr th ur
intrf t be rniv even if there i a rblm with the server, the intrf is
usually writtn in JvSrit nd run lll frm ur computer, while th mr
complex d run n a server.

As with n programming lngug, JvSrit will have bug in it, nd mtim th


will u th program t fil. Frtuntl, unlik a dkt rgrm, whn a JvSrit
rgrm fil, the rt f th g usually till works fin.
Getting Rid of the JvSrit Warning Dilg n Intrnt Exlrr

Mt JvSrit rrr u get are due t a failure in a single wbg. Usually th


rrr r hrml: Although you m lose some functionality, th r nt a dngr to
your mutr.

However, Internet Exlrr pops up a wrning dialog b default. Thi warning stops u
from ding nthing n th g nd n be nning. If u r a rgrmmr it m b
intrting t lk t th d, but for th rt f u it is nt vr uful.
T gt rid f the dilg, fllw th steps:

1. In Internet Explorer, lt Internet Options from th Tl mnu.


2. Click n the Advanced tab.
3. Dnding on your version thr m b up t thr hkbx u nd t set (two
f thm checked, one unchecked):

o Chk: Dibl rit dbugging (Intrnt Explorer)


o Chk: Dibl script debugging (Other)
o Unhk: Dil a notification but vr rit error

4. Click the OK buttn.

Thi huld t the warning dilg from showing. If u trt having rblm with
mn web g, thr may b a rblm with your brwr, nt with the web g, and
you may nd t rintll Intrnt Explorer.
Errr Abut JavaScript

Fr mt mutr ur jvrit is trng PC lingo. Most people except PC


professionals rgrmmr knw littl but it. Essentially, JvSrit i a web
development language that i most mmnl ud on wbit. Popular mng nn-
rgrmmr for it user-friendly nature, JvSrit l llw scripting access to
bjt embedded in other applications.

Hwvr, JvSrit errors r firl mmn too, lrgl bu mn mtur


write web dvlmnt programs uing it. But the terrible thing is tht it can l be
used t writ dwr and spyware, which mk thir w into your mutr, ftn
withut you realizing it, and thrb clog the registry. Thi in turn slows dwn your
mutr nd m even u it t rh, nd we ll knw what a big hdh tht
means.

Th common JvSrit errors r fllwing:


1. JavaScript is nt nbld in th browser.
2. Th wb g ntin a JvSrit programming rrr.
3. Th brwr d not urt JavaScript thnlg.

Wht n w do t deal with th problems? Of ur, u uld h t infrm th


Wbmtr about the problem and supply ll dtil of th jvrit rrr mg that
you hv bn riving, but u hv n ir ltrntiv t tht. Siml ln ur
windows rgitr and wth th rblm vanish.

Fr those not in th knw, the windw registry i a vast internal dtb in which ll
rgrm tht you u n your tm, including windw itlf, store a hug vlum f
dt. S whnvr u mk a hng t th Control Panel tting, fil itin,
system lii, r intll nw ftwr, th hng reflect in the rgitr. So the
rgitr tull mnitr the w ur tm bhv, nd unl u keep windows
rgitr ln b using Rgitr Clnr tool rgulrl, you will nt gt w from
JvSrit rrr mg.
E Tips You Cn U t Remove a JavaScript
Error

Many wbit buildr rl n JvSrit to make their websites functional. Functions


such dr dwn mnu, img hng, and linking t other wbit r ll dn
thrugh th JavaScript rgrm. Whn a JvSrit error up, tht mn that there
is mthing incorrect in the ding.

Ltting JvSrit Errors Run on Yur Wbit Could Ct Money

Exrining a JvSrit rrr n b devastating for some wbit builders. It is a


mmn error, but vn a iml error can ruin th utm f the wbit and mk it
nfuing and frutrting for th nd ur. Thi can, in turn, t th buildr money
wll lose buin.

Dibl Error Alrt fr th Firefox Brwr

On w to hl brk th constant l f u rrr lrt is t manually change th


tin to nbl r disable lrt message ups. To dibl these lrt fr th
Mzill Firfx ur do th following:

1. Select th "Tl" tb t th t f th page


2. Click "Options" and lt th "Content" tab t th t f the box
3. Disable the lrt by unhking the box "Enbl JvSrit"
Thi will dibl th lrt frm intrruting u n th intrnt. However, vn though
th lrt r dibld, it d nt mean that the rrr are gn for good.

Tr Uing a Diffrnt Web Brwr

Different web brwr rt diffrntl t different websites. Whr n brwr


might nuntr n rrr, th other will nt. If th rrr still ur thrugh other
brwr, try uing someone else's unt nd vn mn else's mutr. If the
errors stop when using mn l' PC, thn it m b time to fu n lning ur
PC' registry.
Remove Temporary Internet Files from Intrnt Explorer

Your internet browser makes i f the places you go and stores thm in temporary
fil. A build up f these fil n rt rrr in th system nd th nd t be
mtid ridill. For Intrnt Explorer users do th following:

1. Slt th "Tl" tab ltd at th top f th g


2. Slt th "Gnrl" tb
3. Undr "Tmrr Internet Files" lt "Stting"
4. Click "Dlt Fil" and hit "OK"
5. Click "Delete Cki" nd hit "OK"
6. Also, undr "History" lt "Clr Hitr"
7. Slt "Y" nd then click "OK"
Maintain Your PC' Registry

Fix th JvSrit rrr if it till persists with a registry lnr. Cntnt u of a PC


n wear it dwn nd th constant flw f infrmtin n lg up ur registry, slowing
dwn ur computer. Th errors can rult from a fult or rrut rgitr.

Optimize-Your-PC n rvid u with dvnd rgitr lning thnlg. It will


scan, dign, nd repair ur JavaScript Errr, lving u with nothing but a highl-
timizd PC.
Adding JvSrit in Drmwvr 8

JvSrit llw u t dd intrtiv elements to nd perform ritd funtin n


Wb pages. Adding JvSrit in Drmwvr 8 is not difficult, nd does not ruir
u t knw JavaScript programming.

In Drmwvr 8, JvSrit i a "bhvir" nd i ntrlld thrugh th Bhviur


panel. Yu'll use th Behaviours nl when dding JavaScript in Drmwvr 8 to a
Web g u'r working with.

T n th Behaviours nl, select Windw > Behaviours. Slt th + button to


tivt a drop-down mnu. Th Add Behaviours dr dwn mnu ntin but two-
dozen mmn JvSrit-ntrlld funtin u m dd t a g in Drmwvr.
Common funtin like hking browser types nd rviin, hking for a lug-in,
diling a P-u mg, validating a frm or ging t a URL are examples f
JvSrit bhviur u m add.

Eh bhviur in the Add Bhviur menu h a dilgu bx tht nbl u to


if the tin tht huld tk l whn a rtin triggr is nuntrd. A trigger
i n tin tht a viitr t ur Wb it m rfrm, uh liking n a link,
ntring dt into a frm r muing vr a rtiulr location n th page.

You'll find th Vlidt Form bhviur uful if u collect td infrmtin frm


ur viitr. Th Vlidt Frm bhviur n used to verify that data entered b a
visitor i in the rrt format r ntin nl valid dt. On u have nfigurd th
bhviur rritl, lt OK.

If th bhviur u want t add i not among the built-in JvSrit bhviur, u


may select Get Mr Bhviur. Thi ltin will tk u t th Mrmdi
Drmwvr Exhng Sit. Hr u'll find ur-rtd JvSrit bhviur tht
u m download. On utin: nt ll behaviours t thi ltin are free f hrg,
but you'll find that adding nw behaviours t ur Drmwvr intrf is when
you viit this site.

You m l change a bhviur u have ddd t Drmwvr 8. T mdif an


existing bhviur, find th behaviour u want t dit in Cd View. Slt it and it
will appear in th lit blw the Add Bhviur buttn in th Behaviours nl. Frm
thi int, you m mdif th rdr in whih events ur, hng the vnt tht triggr
an action r dlt th behaviour ltgthr.
How Gd I JvSrit fr Building a Large
Sl Wb Alitin?

Arding to the mt recent ttiti posted n w3th.m, more thn 89% f


wbit currently use JvSrit a client-side programming language. A a r-
ltfrm and lightwight rgrmming lngug, JvSrit makes it ir for
rgrmmr t build rniv wbit nd wb applications that wrk with
mll with ulr web browsers, rting tm and devices. It is l widl
used b programmers part f the wb brwr t accomplish important tk like
brwr control, user interaction, and asynchronous mmunitin.

Also, the programmers have tin t u JvSrit as a server-side riting lngug


through Nd.j and imilr runtime nvirnmnt. At th same tim, th l n u a
vrit of n ur frmwrk t rdu th munt f tim and effort ruird for
building JvSrit litin. It n l be ud lng with HTML5 and CSS for
rting wb applications and gm. S u n lw nidr uing JvSrit a
dnmi client-side programming language for building lrg l wb applications.

Advntg f Uing JvSrit fr Lrg Scale Wb Alitin Development


Supported b Mjr Web Brwr

JvSrit i urtd by mt of th widl used wb brwr including Firfx,


Chrome, Intrnt Explorer, Sfri nd Or. It i l urtd b th nw web
brwr whose vndr have imlmntd JvSrit. S th ur can th wb
applications uing JvSrit rgrdl f their choice of wb brwr. They also hv
option to ll functionality of th wbit b nbling th riting lngug if it i
disable du to some rn.
N Nd t Use An Sifi Tools

JvSrit is n intrrtd rgrmming language. So u can easily write th d


withut uing any specific tl r rgrm. Yu n simply n a ntd, nd trt
writing JvSrit d. Al, u have tin t u a number f ditr t idntif th
mistakes in th d. The ditr colorize or rfrmt th script t make it ir fr
ur t identify th rrr.

Otin to Reuse th Code

Yu n furthr ru th JvSrit d r multil g iml b ling th


d in rt fil. Aftr placing the d in a rt fil, u hv to v th fil
with.js extension. Th fil can b linked t multil wb pages by uing the <rit> tag
t the HTML d f the g. Th option nbl you to vid writing additional d
while dding the m functionality to various rt f th wbit.

Mn Librri nd Frmwrk

You hv option t reduce th tim and effort required fr building lrg JavaScript
litin b uing several frmwrk nd libraries. Mn rgrmmr rfr using
dnmi JvSrit frameworks lik AngularJS, Bkbn, Ember and React. Hwvr,
you till hv tin to choose from a lng lit f JavaScript libraries rding t your
ifi needs. For intn, u n fftut GUI development uing widgt lik
DHTMLX, Bttr, jQur UI, DjWidgt or AngulrJS. Likewise, you u ulr
template tm like jQur Mobile, Hndlbr, Mustache nd Cd Frmwrk.

Cmrtivl Ftr

A ntd earlier, JvSrit i a client-side riting lngug. S the d i executed


n ur' tm. A th code is executed withut n rvr interaction, th ring
i dn t a comparatively faster. The faster ring nbl th wb litin t
keep th users engaged b delivering rihr brwing xrin.
Dlivr Updated Response Data frm th Server

JvSrit further u the XmlHttRut API fr data rtrivl. The API nd HTTP
or HTTPS requests to th web rvr, nd load the rn nt b th rvr in th
script. Th rit n il udt the urrnt wb page bd n th rn dt
received from th rvr without rlding a nw web g. S u can boost the web
application's rfrmn b rviding updated infrmtin to the ur without
rlding the wb page mltl.
Extnd th Wbit' Funtinlit

JvSrit mk it easier for u to extend th funtinlit f the wb application


withut putting any xtr effort. You can u th riting lngug to rt visual
fft n the rn, lult data, and mk web pages mr intrtiv. JavaScript
further llw you t xtnd the website's funtinlit b using third-rt scripts. Th
flexibility makes it ir fr you t dd out-of-box features t the web litin
without using any dditinl lug-in r tl.

Reduces Ld n Srvr

Th rvr-id riting languages ruir th wb server to process th ur request


bfr rn i nt to th ur. So th process ruird additional tim dit th
ur hving a high-d intrnt nntin. But the JvSrit code i rd n
th ur' mhin, withut interacting with the web browser. Thu, th trin and ld
n th web server is rdud drastically, whil th ur gt uik rn. Yu n
u JvSrit whil building lrg scale websites to rvid ftr rn to users
withut putting x trin n th rvr.

Otimiz Local Ching

JvSrit n be used lng with HTML5 t nbl users to access websites vn


whn thr i not active intrnt connection. Th technologies r fftiv in hing
dt locally, nd k th wb litin responsive till th intrnt connection is
rtrd. Yu n vn use tl like Knd UI DtSur t trk hng md t an
object lll, nd upload the hng to the web server n the ntwrk nntin i
restored. Th option nbl u to make th web applications work jut like mbil
.
Wrk with Othr Wb Thnlgi

As mentioned earlier, JavaScript dvlr n b ud bth lint id and rvr


id scripting language. At the m tim, u l hv tin to u JvSrit a
lint side thnlg lng with m f th widl ud wb technologies lik PHP,
Perl nd Java. Th riting language i also used widl along with HTML5 nd CSS
fr developing responsive wbit accessible n a vrit of dvi uing th m
d b.

Hwvr, you mut keep in mind m f th mjr shortcomings f JavaScript whil


uing it for building lrg wbit and web litin. A JavaScript d i
executed n the web brwr, it bm vulnrbl t vriu security ttk. Al,
th code bm unrditbl as individul browsers interpret it diffrntl.

Mjr bt of major rh engines inluding Google nnt undrtnd th client-side


d writtn in JavaScript completely nd fully. So th search engine ranking nd
viibilit f the wb litin uing t much JvSrit will be imtd. But u
can till use JavaScript lng with other ulr wb technologies lik HTML5 and
CSS t rt robust wb litin accessible n multiple dvi, rting
tm nd web brwr.
Why Wuld Cmni Hire JavaScript
Developers?

Cmni r f late utting mr mhi on hiring JavaScript developers. Th


buin are nwd nding a good deal of money, time well efforts t
rruit talented, ill th ungr nd fresher n minl for thir ulit f
xrti and bu of th ft that th are in tune with th ltt technology and trnd
f dvlmnt.

Nw, thr r a numbr f reasons wh mdrn businesses r putting their mn n


JavaScript dvlr.

The rimr rn wh buin bank n these i their bkgrund dutin. Most


of ndidt hv a bachelor's degree in software engineering, computer science,
information thnlg nd other dgr tht r rtinnt with th nih of buin
that i run b the companies.

However, rtin buin ut mr emphasis n the experienced, rthr thn th


dgr th . Tht is a diffrnt issue altogether thugh.
Businesses ut money because f a solid undrtnding f th ftwr dvlmnt lif
l whih th rfinl hv. Plus, they have a strong nt - a fact tht
ugmnt thir rgrmming kill fr th client-side well server-side languages.
Thi i xtrml important nt vr project r client nd nl JvSrit but the
nillr rgrmming platforms wll. Moreover, when it m to rnl traits,
th rfinl have xtrml strong analytical kill along with trubl hting
xrti - a fact tht m in xtrml hnd in software development. Mrvr,
th hv th needed psychological gtu t work in sync with thr in a tm. Tht i
itself a reason nugh fr companies to hire thm.

Apart frm all th issues, good t knw thir responsibilities nd have n excellent
professional utlk that mk all th diffrn. Th mjrit f th JvSrit
dvlmnt rgrm r particularly designed as well as developed for web-based
lng with diffrnt rvr-bd applications tht r gnrll used n web it and
mutr systems.
Aftr dvlmnt, it i nr t evaluate th programs properly. A gd dvlr
wuld also have the xrti t vlut th rgrmming scripts nd hn,
mni hir them to ut t a rgrmmr will b able t develop wll
vlut th scripts. They are also xrt in testing the lngug xtnivl t see
whthr it i funtining properly bfr it i hndd vr t th lint. Th also nur
that th script i ttll fr of bug.

Th professionals, ill th frh ut have an extensive knwldg n the


ltt Jv Virtul Mhin r JVM lngug nd this helps thm complete th tk
faster. Th are l are expert in building an application that will make th lngug
simpler. This at th end of th day saves a lt of money for the companies.

Th k for the mni i t lk fr the bt nginr who r invlvd in th


software dvlmnt mmunit so that th r in tune with the latest technology and
are frh frm th int f viw f concept-creation. This i t make ur tht th
buin gt mximum return frm th invtmnt th ut n JavaScript dvlr
wh are the mint f buin dvlmnt.
Exrt r nt only killd nd experienced, but th r rfiint in thr lngug
wll, like , C++, Nt, Pthn, Joomla nd so on. Th hv imbl skills. S,
though hiring uh a rfinl is uit cost-effective, tking int unt td'
infltin, till companies vie on them thi pays off in th hrt as wll as long run
ubuntl. Hwvr, in rdr t save costs f lt mni are grwing a
propensity f hiring ffhr rfinl instead f employing individul on a full
tim bi. These rfinl ffr maintenance wll as thr nillr rvi t
no dditinl t and thi hl th mni t ut t t a lrg xtnt.
JvSrit Where T

Th <script> Tag

In HTML, JvSrit d mut be inrtd btwn <script> nd </rit> tg.

Example

<rit>
document.getElementById("demo").innerHTML = "My Firt JavaScript";
</rit>

Old JvSrit xml may use a t ttribut: <rit t="txt/jvrit">.


Th t ttribut is not ruird. JvSrit i th dfult riting lngug in HTML.

JavaScript in <head> r <body>


Yu can place n numbr of rit in n HTML dumnt.

Scripts n be ld in th <body>, r in th <hd> tin of n HTML g, r in


both.

JavaScript in <hd>

In this example, a JvSrit function i ld in th <hd> section f an HTML g.

Th function is invkd (lld) whn a buttn i likd:

Exml
<!DOCTYPE html>
<html>
<hd>
<rit>
funtin myFunction() {
dumnt.gtElmntBId("dm").innrHTML = "Paragraph hngd.";
}
</script>
</hd>

<bd>

<h1>A Wb Pg</h1>
<p id="dm">A Paragraph</p>
<buttn type="button" nlik="mFuntin()">Tr it</button>

</bd>
</html>

JvSrit in <body>

In thi xml, a JvSrit funtin i placed in the <bd> section of n HTML page.

Th funtin i invoked (lld) whn a buttn i clicked:

Example
<!DOCTYPE html>
<html>
<body>

<h1>A Web Pg</h1>


<p id="dm">A Prgrh</>
<button t="buttn" onclick="myFunction()">Try it</button>
<rit>
funtin mFuntin() {
document.getElementById("demo").innerHTML = "Prgrh changed.";
}
</rit>

</bd>
</html>

Extrnl JvSrit

Extrnl fil: mSrit.j


function myFunction() {
dumnt.gtElmntBId("dm").innrHTML = "Paragraph hngd.";
}
Extrnl rit r rtil whn the m d is ud in many different wb pages.

JavaScript files hv the fil extension .j.

T u n xtrnl rit, ut th nm f the rit file in th r (ur) attribute f a


<script> tg:

Example
<!DOCTYPE html>
<html>
<body>

<script r="mSrit.j"></rit>

</bd>
</html>

Yu can l an xtrnl rit rfrn in <head> r <body> u lik.

Th rit will bhv as if it w located xtl where th <script> tag is located.


Extrnl rit cannot ntin <rit> tags.

Extrnl JavaScript Advntg


Pling rit in external fil has m dvntg:

It rt HTML nd d
It mk HTML nd JvSrit ir t read nd mintin
Chd JavaScript fil can d u page ld
T add vrl script fil t n page - use vrl rit tg:

Example
<script r="mSrit1.j"></rit>
<script src="myScript2.js"></script>
Extrnl Rfrn
External rit n be rfrnd with a full URL or with a th relative t the urrnt
wb page.

This example u a full URL t link to a rit:

Exml
<rit r="htt://www.w3hl.m/j/mSrit1.j"></rit>

This xml link to a rit ltd in th m folder as th urrnt g:

Exml
<script src="myScript1.js"></script>
JvSrit Output

JvSrit Dil Possibilities


JavaScript n "display" dt in diffrnt w:

Writing int n lrt bx, using window.alert().


Writing int the HTML utut uing document.write().
Writing int an HTML lmnt, uing innrHTML.
Writing into th brwr console, uing nl.lg().
Uing windw.lrt()
You can u n lrt bx t dil dt:

Exml
<!DOCTYPE html>
<html>
<body>

<h1>M Firt Wb Page</h1>


<>M firt rgrh.</>

<rit>
windw.lrt(5 + 6);
</script>

</bd>
</html>

Using dumnt.writ()
For tting ur, it i convenient to u document.write():
Exml
<!DOCTYPE html>
<html>
<bd>

<h1>M Firt Web Pg</h1>


<>M firt rgrh.</>

<script>
dumnt.writ(5 + 6);
</rit>

</bd>
</html>

Uing document.write() ftr n HTML dumnt is fully ldd, will delete ll xiting
HTML:

Exml
<!DOCTYPE html>
<html>
<bd>

<h1>M First Wb Page</h1>


<p>My firt rgrh.</>

<button nlik="dumnt.writ(5 + 6)">Try it</buttn>

</body>
</html>

Uing innrHTML
T n HTML lmnt, JavaScript n u the dumnt.gtElmntBId(id)
method.

The id ttribut dfin th HTML lmnt. Th innrHTML property dfin th HTML


ntnt:

Example
<!DOCTYPE html>
<html>
<body>

<h1>M Firt Web Pg</h1>


<p>My First Prgrh</>

<p id="dm"></>

<rit>
dumnt.gtElmntBId("dm").innrHTML = 5 + 6;
</script>

</bd>
</html>

T "display data" in HTML, (in mt ) u will set th value of an innerHTML


rrt.

Uing console.log()

In your browser, you n u th nl.lg() method t display dt.

Example
<!DOCTYPE html>
<html>
<bd>

<h1>My Firt Web Pg</h1>


<>M firt paragraph.</p>

<rit>
nl.lg(5 + 6);
</script>

</bd>
</html>
JvSrit Syntax

JvSrit syntax i th t f rul, hw JvSrit rgrm are ntrutd.

JvSrit Programs
A mutr program is a lit f "intrutin" t b "executed" b the computer.
In a programming language, th rgrm intrutin r lld statements.

JvSrit i a programming language.

JvSrit ttmnt r rtd b semicolons:

Exml
vr x, , z;
x = 5;
y = 6;
z = x + ;

In HTML, JvSrit rgrm r xutd b th web brwr.

JvSrit Statements
JvSrit ttmnt are md f:

Values, Ortr, Exrin, Kwrd, and Cmmnt.

JvSrit Vlu
The JavaScript ntx defines tw t of vlu: Fixd vlu and vribl values.

Fixed vlu are called litrl. Variable values are lld variables.
JavaScript Literals
Th mt imrtnt rul fr writing fixed vlu r:

Numbr are written with r withut diml:

10.50

1001

Strings r txt, writtn within dubl r ingl quotes:

"Jhn D"

'John Doe'
JvSrit Vribl
In a rgrmming lngug, vribl r ud t tr dt vlu.

JavaScript u th vr keyword to dlr vribl.

An ul ign is ud t assign values t vribl.

In thi xml, x i dfind as a variable. Then, x i ignd (given) th vlu 6:

var x;

x = 6;
JavaScript Operators

JvSrit uses n ignmnt operator ( = ) t ign vlu t vribl:

var x, ;
x = 5;
y = 6;
JvSrit u rithmti operators ( + - * / ) to mut vlu:

JvSrit Expressions

An expression i a mbintin of vlu, vribl, and operators, whih mut t a


vlu.

Th muttin i lld n vlutin.

Fr xml, 5 * 10 evaluates t 50:

5 * 10

Exrin n l ntin variable vlu:

x * 10

Th values can b f various t, such numbr and strings.

Fr example, "Jhn" + " " + "D", vlut t "Jhn Doe":

"Jhn" + " " + "D"


JvSrit Kwrd

JvSrit kwrd r ud t idntif tin to b rfrmd.

The vr kwrd tll th browser to create variables:

vr x, y;
x = 5 + 6;
y = x * 10;

JavaScript Cmmnt
Not ll JvSrit statements r "executed".

Cd ftr double slashes // r btwn /* and */ i trtd a mmnt.

Cmmnt are ignrd, nd will nt b executed:

vr x = 5; // I will be executed

// var x = 6; I will NOT be xutd


Try it Yurlf
Yu will lrn mr about comments in a ltr chapter.

JvSrit Identifiers

Idntifir r names.

In JvSrit, idntifir r ud t nm vribl (and kwrd, and functions, nd


lbl).

Th rul fr lgl nm r muh th same in most programming lngug.


In JavaScript, th firt character mut b a letter, n underscore (_), or a dollar sign ($).

Subunt hrtr may be lttr, digit, underscores, or dollar signs.

Numbers r nt llwd as th firt hrtr.


This w JvSrit n il ditinguih idntifir frm numbr.

JvSrit i Case Snitiv


All JvSrit idntifir r nitiv.

The vribl ltNm nd lastname, are two diffrnt vribl.

vr lastname, ltNm;
lastName = "D";
ltnm = "Ptrn";

JavaScript d not intrrt VAR r Vr th keyword vr.

JvSrit nd Camel C

Hitrill, rgrmmr hv ud three ways f joining multiple wrd into one


vribl name:

Hhn:

firt-nm, last-name, mtr-rd, intr-it.

Hyphens r not llwd in JvSrit. It i rrvd fr ubtrtin.

Underscore:

first_name, lt_nm, mtr_rd, inter_city.


Cml C:

FirtNm, LtNm, MtrCrd, IntrCit.

mlC

JvSrit rgrmmr tnd to u camel tht trt with a lowercase letter:

firtNm, ltNm, mtrCrd, intrCit.

JvSrit Character St

JvSrit u th Unicode character t.

Unicode covers (lmt) ll the characters, punctuations, nd symbols in the wrld.


JvSrit Sttmnt

This ttmnt tll the browser t writ "Hello Dll." inid an HTML element with
id="demo":

Exml

dumnt.gtElmntBId("dm").innrHTML = "Hll Dll.";

JvSrit Prgrm
Most JavaScript programs ntin many JvSrit ttmnt.

Th ttmnt are xutd, one b one, in the same rdr they r written.

In thi xml x, y, nd z r givn values, nd finally z i displayed:

Exml
vr x, , z;
x = 5;
y = 6;
z = x + ;
document.getElementById("demo").innerHTML = z;

JvSrit programs (and JvSrit ttmnt) r ftn called JvSrit code.

Smiln ;

Smiln rt JvSrit ttmnt.


Add a semicolon at the nd of h xutbl ttmnt:

vr , b, ;
a = 5;
b = 6;
c = a + b;

When separated b miln, multil ttmnt n n lin are allowed:

a = 5; b = 6; c = a + b;

On the web, u might see xml withut semicolons.

Ending ttmnt with semicolon i nt ruird, but highly rmmndd.

JavaScript White S

JvSrit ignores multil spaces. Yu n add whit t ur rit t mk it


more readable.

The fllwing lines r uivlnt:

vr rn = "Hege";
var rn="Hg";

A good rti is t put rund rtr ( = + - * / ):

vr x = y + z;
JvSrit Line Length nd Lin Brk

Fr best rdbilit, rgrmmr ftn lik t vid d lin longer thn 80


hrtr.

If a JvSrit ttmnt d nt fit n one line, the best l t break it, i after an
operator:

Exml
document.getElementById("demo").innerHTML =
"Hll Dll.";
JvSrit Code Blk
JavaScript ttmnt n be grouped tgthr in code blk, inid url brkt
{...}.

The purpose of code blocks is t define ttmnt t b xutd together.

On l u will find ttmnt grud together in blk, i in JvSrit


funtin:

Example

funtin mFuntin() {
document.getElementById("demo1").innerHTML = "Hello Dolly.";
dumnt.gtElmntBId("dm2").innrHTML = "How are u?";
}
JavaScript Keywords

JvSrit statements often trt with a keyword t idntif th JvSrit tin t b


performed.

Hr i a lit f some f the kwrd you will lrn but in thi tutorial:

Kwrd Dritin
brk - Trmint a with or a l

ntinu - Jum ut of a l nd trt t the t

dbuggr - St th xutin f JvSrit, and ll (if vilbl) the


debugging funtin

do ... while Exut a blk f ttmnt, and repeats th blk, whil a condition
is true

fr Mrk a blk f statements t b xutd, long as a condition i tru

function Dlr a function

if ... l Mrk a block of ttmnt t b xutd, dnding on a nditin


return Exit a function
with Marks a blk f statements to be xutd, depending n different cases
tr ... th Imlmnt rrr hndling t a block f ttmnt
vr Dlr a vribl
JavaScript Cmmnt

JvSrit comments n be ud t explain JavaScript code, nd t mk it more


readable.

JavaScript mmnt n also b ud t rvnt execution, whn testing ltrntiv


code.

Singl Lin Cmmnt


Single lin mmnt trt with //.

Any text between // and th end of the line will b ignrd b JavaScript (will not b
executed).

Thi example u a ingl-lin mmnt before h d lin:

Exml

// Chng hding:
dumnt.gtElmntBId("mH").innrHTML = "M First Pg";
// Chng paragraph:
document.getElementById("myP").innerHTML = "M firt rgrh.";

Thi xml uses a ingl line mmnt t th end f h lin t explain th code:

Exml
var x = 5; // Declare x, giv it th value of 5
var y = x + 2; // Dlr , give it th vlu of x + 2

Multi-line Cmmnt
Multi-lin mmnt trt with /* nd nd with */.
Any txt btwn /* nd */ will b ignored b JvSrit.

This xml u a multi-lin comment (a mmnt blk) to xlin th code:

Exml
/*
Th d blw will hng
th heading with id = "myH"
and th rgrh with id = "mP"
in m wb g:
*/
dumnt.gtElmntBId("mH").innrHTML = "M Firt Page";
dumnt.gtElmntBId("mP").innrHTML = "My first rgrh.";

Uing Comments to Prvnt Execution


Uing mmnt to prevent xutin of d i uitbl fr d tting.

Adding // in frnt of a d lin changes th code lin frm n executable line to a


comment.

Thi xml u // to rvnt xutin of n of th d lin:

Exml
//dumnt.gtElmntBId("mH").innrHTML = "M Firt Pg";
dumnt.gtElmntBId("mP").innrHTML = "M firt rgrh.";

Thi xml uses a mmnt block t prevent execution f multil lin:

Exml
/*
document.getElementById("myH").innerHTML = "M Firt Pg";
dumnt.gtElmntBId("mP").innrHTML = "M first paragraph.";
*/
JvSrit Vribl

JvSrit Variables
JavaScript vribl r ntinr fr tring dt values.

In thi xml, x, y, nd z, r vribl:

Exml
var x = 5;
vr y = 6;
vr z = x + y;

Frm th xml bv, u can expect:

* x tr the value 5
* y tr th vlu 6
* z tr th vlu 11

Muh Like Algbr


In thi xml, ri1, ri2, nd total, r variables:

Exml
var ri1 = 5;
vr price2 = 6;
var total = ri1 + ri2;

In rgrmming, jut like in lgbr, we use variables (lik price1) to hld values.

In rgrmming, jut lik in lgbr, we u vribl in xrin (ttl = ri1 +


ri2).
From th xml bv, u can lult th ttl to b 11.

JavaScript vribl r ntinr fr tring dt values.

JavaScript Idntifir

All JvSrit vribl mut b identified with uniu names.

These unique names r lld idntifir.

Idntifir can b hrt names (lik x and ) r mr dritiv names (age, um,
totalVolume).

The general rul fr constructing names fr vribl (uniu idntifir) are:

* Nm can ntin lttr, digit, underscores, nd dollar ign.


* Names must bgin with a letter
* Names n also bgin with $ nd _ (but w will not use it in thi tutril)
* Nm r nitiv ( nd Y r different vribl)
* Reserved words (like JavaScript kwrd) nnt b ud names

JavaScript idntifir r case-sensitive.

Th Aignmnt Operator
In JvSrit, th equal ign (=) i n "ignmnt" operator, not n "ul to" operator.

Thi i diffrnt from algebra. The fllwing does not mk sense in algebra:

x=x+5
In JvSrit, hwvr, it mk rft n: it ign th vlu of x + 5 to x.

(It calculates the vlu f x + 5 nd puts th rult int x. Th vlu f x is inrmntd


by 5.)
The "equal t" operator i written lik == in JvSrit.

JvSrit Data Types


JvSrit vribl n hold numbr lik 100 nd txt values lik "John D".

In rgrmming, txt vlu r lld txt strings.

JavaScript can handle many types f data, but fr nw, jut think of numbers and tring.

Strings r writtn inside dubl r ingl ut. Numbers r written withut quotes.

If u ut a number in ut, it will b trtd a text tring.

Exml

vr i = 3.14;
vr rn = "John Doe";
vr nwr = 'Y I m!';

Declaring (Creating) JvSrit Vribl

Crting a vribl in JavaScript i lld "dlring" a variable.

You dlr a JvSrit vribl with the vr keyword:

var carName;
After the dlrtin, th variable has n value. (Thnill it h th vlu f
undefined)

T assign a value t th vribl, use the ul ign:

rNm = "Vlv";

Yu n also ign a vlu to the vribl whn you declare it:


var carName = "Vlv";
In th xml below, w create a vribl lld rNm and ign th value
"Vlv" to it.

Then we "utut" th vlu inid n HTML rgrh with id="dm":

Exml
< id="dm"></>

<rit>
var rNm = "Volvo";
dumnt.gtElmntBId("dm").innrHTML = carName;
</rit>

On Statement, Mn Variables
You n dlr mn vribl in one ttmnt.

Strt th ttmnt with vr and separate th variables b mm:

var rn = "Jhn Doe", rNm = "Vlv", ri = 200;

A dlrtin can n multil lin:

var person = "Jhn D",


carName = "Vlv",
ri = 200;

Vlu = undfind

In computer programs, vribl r ftn dlrd without a vlu. The vlu n be


something tht has to b calculated, r mthing tht will be provided later, like ur
input.
A variable declared withut a vlu will have the value undfind.

Th vribl rNm will have the value undefined after th xutin f this
statement:

Example
vr rNm;

R-Dlring JavaScript Variables


If you re-declare a JvSrit vribl, it will not l it vlu.

The vribl rNm will till have the value "Vlv" after th xutin f these
statements:

Example
vr rNm = "Vlv";
vr rNm;

JvSrit Arithmetic
As with algebra, you can d rithmti with JvSrit vribl, using operators lik =
nd +:

Example
var x = 5 + 2 + 3;

You can l dd tring, but strings will be ntntd:

Example
var x = "Jhn" + " " + "Doe";

Al tr this:

Exml
vr x = "5" + 2 + 3;
JavaScript Ortr

Example
Aign vlu t vribl nd dd thm tgthr:

var x = 5; // ign th vlu 5 to x


vr y = 2; // ign th value 2 to y
var z = x + y; // assign th value 7 to z (x + y)

The assignment operator (=) ign a value t a vribl.

Aignmnt
var x = 10;

Adding
vr x = 5;
vr y = 2;
vr z = x + ;

Th multilitin operator (*) multiplies numbers.

Multiling
var x = 5;
vr y = 2;
var z = x * ;

JavaScript Arithmti Ortr


Arithmti operators are used t rfrm rithmti n numbr:

Ortr Dritin
+ Additin
- Subtrtin
* Multilitin
/ Diviin
% Modulus
++ Inrmnt
-- Drmnt

Th addition ignmnt rtr (+=) dd a vlu t a vribl.

Assignment
var x = 10;
x += 5;
Aignmnt operators r full described in th JS Aignmnt htr.

JvSrit String Ortr


Th + rtr n also b ud t dd (ntnt) strings.

Exml

Th += assignment rtr can also b ud t dd (ntnt) tring:

Exml
txt1 = "What a vr ";
txt1 += "ni d";
Th rult f txt1 will b:

Wht a vr ni d

Whn ud n tring, th + rtr i lld th concatenation rtr.

Adding Strings and Numbr


Adding tw numbr, will rturn the sum, but dding a number nd a tring will rturn a
tring:
Exml
x = 5 + 5;
y = "5" + 5;
z = "Hll" + 5;
The rult f x, y, and z will b:

10
55
Hll5

If u dd a number and a tring, th rult will b a tring!

JvSrit Cmrin Ortr


Operator Dritin
== ul to
=== ul vlu nd ul type
!= nt equal
!== nt equal vlu or nt ul type
> grtr thn
< less thn
>= greater thn or ul t
<= l thn or ul to
? ternary rtr

JvSrit Lgil Ortr


Operator Dritin
&& lgil nd
|| lgil or
! lgil nt

JavaScript Type Ortr


Ortr Description
typeof Returns th type f a vribl
intnf Returns true if n bjt is an instance f n bjt t
Jv nd JavaScript: Not th Same

A mmn rblm for rgrmmr i nfuing th two rgrmming languages Jv


nd JvSrit. Whil th tw languages share
m imilriti, th are vr diffrnt beasts nd gnrll ud in different
applications.

Java i a general-purpose programming lngug dvld by Sun Mirtm a


w t lt rgrmmr "writ-n, run
nwhr". Jv d i executed inid a virtul mhin, a Java application n be
ditributd withut th developer hving t
worry but the different architectures and operating tm tht the litin might
run n.

JvSrit, on th other hnd, is a scripting lngug. It' only ur i inid f wb


g. Whn a wb browser loads the page, it
xut th JavaScript d inside it. The d can hng hw th wb g lk
and rt. For example, JavaScript code n be
used t rt a dr dwn menu tht expands whn u mu vr it, r n img
gllr whr th dild img hng vr so
often.
Thi difference i most obvious in the librri f th two lngug. Jv h a lrg
standard librr tht' been standardized in the
Java tndrd. Thi llw Java programmers t u a wid variety f tools whn
writing d. JvSrit' tndrd librr is muh
smaller and gnrll rltd to working with the HTML d in th wb page.

Furthr confusing th tw lngug i th ft that Java can l b ud in a web g


in th form of a Jv lt. This is a mll
rt f th wb g whr Java d runs nd i dild in th browser windw.
Java lt r gnrll used t rt more
dvnd thing in th brwr, a gm fr xml. JavaScript i not nrl wrful
nugh t rt a complex gm, Jv is used.
Thi is whr Jv' ltfrm-indndnt dvntg m in. A lng as th wb
browser has th Jv lugin (and mt d), the d
will run th same n matter which wb brwr i ud. Thi cannot be id fr
JvSrit, whih i ftn imlmntd diffrntl in th
diffrnt web brwr, ruiring various mtibilit hk.
And despite th ft tht bth can b ud web lngug, JvSrit i vtl mr
imrtnt fr th iring wb programmer t
learn. JavaScript's imliit l means that it runs uikl, so tht small things you
may wnt t dd to a wb g rn't bggd dwn
b Java's virtul mhin. Thi i a rn why Jv in't used muh n th wb
anymore: bu it's lw. A Java lt requires
loading th lrg Jv plugin nd is overkill fr ll but lrg wb rjt, especially
when mr flxibl tools uh HTML5 r
bming mr widely ud on the wb.

Jv and JavaScript r tw diffrnt rgrmming lngug. Eh f them have their


u,but th r not interchangeable with h
thr.
How t Use JavaScript t Get Ur Entry nd U
th Entr in a Clultin

Th use f vribl r k to ll programming lngug. Yu m remember the x'


nd y's frm your high school algebra l. In
rgrmming, variables hv even mr meaning than jut some vgu idea f n
unknwn value. In programming, a variable i ud as th nm of a particular
infrmtin storage area. Yu can thn u that variable nm t ll whtvr u
hnd t store thr. If u throw w th old stuff nd ut mthing new in th
trg are, th vribl calls th nw tuff.

Some rgrmming languages hv to knw hd f tim wht u plan t tr. Th


lik t ln hd f tim wht size trg area u are renting. Telling these lngug
wht you ln to store means that u hv t declare a dt t. Th types f
programming languages r called trngl td lngug. Othr lngug, such
JvSrit, uld r l wht kind of tuff you r storing.
They did wht iz space u nd whn you nd it t the trg area. Tht i n
rn JvSrit i l mlitd to learn thn m rgrmming lngug.

Hwvr, there r still m thing u will nd to lrn but dt types bu


thr are some id fft t nt tlling wht type f information u wnt to store.

In n case, th information till h a vribl nm. Hr i n exercise that

1) gathers infrmtin frm a rmt bx,


2) puts it int a vribl,
3) d a lultin, and
4) nd th nwr bk in n lrt.

Add this code to th f a new web g

var SurThi = prompt("Enter a number u would lik t ur.");


vr Answer;

Answer = SurThi * SquareThis;

alert(Answer);
Basic Programming Skill in thi Bit f Code

1. Whn u do any kind f programming, u have t think f what pieces of


infrmtin the mutr will nd to identify and hold n
t. Thn u mk vribl t hld th infrmtin. In this xml, we need t hld
tw i of infrmtin, 1) th number tht will
b urd, 2) the nwr.

2. You tll JvSrit that u want it t tr infrmtin b nming var, a space nd a


vribl name.

3. Th firt lin f thi script d thr jobs, 1) it tells JvSrit to set id memory
fr th vribl SquareThis, 2) it u a r- writtn funtin t rmt th ur
for a numbr, 3) it takes whtvr the ur types in th prompt box and ut it in th
mmr fr SurThi.

4. Notice tht th funtin rmt requires n rgumnt just lik th lrt() did. Nti
too that the argument is in " ", which mn tht the rgumnt i a String data t.

5. Nw the script nd a variable to hold the answer. This i dn with "vr Anwr;"
6. Evrthing is in l nw t d th mth: "Anwr = SurThi * SurThi;" Th
rt that tells the mutr wht kind f mth to d is lld n rtr. Th min
rtr r +, -, *, / (add, ubtrt, multil, divid). There are mn thr rtr,
wll. A ur is any numbr multiplied by itself; thi frmul multili
SquareThis b SurThi.

7. Th rgrm h n answer, but it nd t tll th user. Agin, w will u n lrt


bx.

8. Now change th frmul t do something else. Tr dding nthr vribl nd


nthr alert() to k fr another number (or vrl.)
Here are some rtr for you to xrimnt with:

Ortr

JvSrit Mth Functions

+ Adds tw numbr

- Subtrt tw numbr

/ Divid tw numbers

++ Adds n t a numbr (nl nd n vribl)

-- Subracts one from a numbr (nl nd n vribl)


Cnluin
HTML may nt b used intd f JavaScript. Bth th programming lngug are
mltl different nd hv diffrnt funtin,
ftur nd biliti. Whil HTML is a mrku lngug nd is dignd to rt
tti wb g content, JvSrit is a rgrmming language that can perform dnmi
tasks. It n b ud gin nd again on different pages f a it. This n b dn b
xtrnlizing and ling it in separate fil nd linking a JvSrit fil to the HTML
d of the rtiulr page. Hence, JvSrit n b ddd t mn pages b adding
th rrt tg on h f th pages.

Not vrn wh wn a buin m b wll vrd with JavaScript or know hw t


u it. Using it t create ur it n only bnfit ur it because f the vriu
t tht JvSrit n dd t it. Hn, it wuld lw be hlful if u n hir a
professional web dign company wh n hl you build ur it uing JvSrit.
Th team of professional dignr can sit with u, undrtnd ur buin and
dign a site king ll ur buin nd in mind.

Brian Draper

S-ar putea să vă placă și