Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to JavaScript, covering its history, capabilities, advantages, and limitations. It also discusses data types, operators, and statements in JavaScript, including conditional statements and switch statements. how to embed JavaScript in an HTML file and provides examples of arithmetic and string operators. useful for students studying web development or programming languages.
Typology: Study notes
1 / 58
JavaScript JavaScriptisthepremierclient-side interpretedscriptinglanguage.It‟swidelyusedin tasks rangingfrom thevalidationofform datatothecreationofcomplexuserinterfaces. DynamicHTMLisacombinationofthecontentformattedusingHTML,CSS,Scripting languageandDOM.Bycombiningallofthesetechnologies,wecancreateinteresting andinteractivewebsites.HistoryofJavaScript: NetscapeinitiallyintroducedthelanguageunderthenameLiveScriptinanearlybeta releaseofNavigator 2 .0in1 995 ,andthefocuswasonform validation.Afterthat,the languagewasrenamedJavaScript.AfterNetscapeintroducedJavaScriptinversion2. 0 oftheirbrowser,MicrosoftintroducedacloneofJavaScriptcalledJScriptinInternet Explorer 3. 0. WhataJavaScriptcando? JavaScriptgiveswebdevelopersaprogramminglanguageforuseinwebpages& allowsthem todothefollowing: JavaScriptgivesHTMLdesignersaprogrammingtool JavaScriptcanbeusedtovalidatedata JavaScriptcanreadandwriteHTMLelements Createpop-upwindows Perform mathematicalcalculationsondata Reacttoevents,suchasauserrollingoveranimageorclickingabutton Retrievethecurrentdateandtimefrom auser‟scomputerorthelasttimeadocument wasmodified Determinetheuser‟sscreensize,browserversion,orscreenresolution JavaScriptcanputdynamictextintoanHTMLpage JavaScriptcanbeusedtocreatecookies AdvantagesofJavaScript: Lessserverinteraction Immediatefeedbacktothevisitors Increasedinteractivity Richerinterfaces Websurfersdon‟tneedaspecialplug-intouseyourscripts JavaScriptisrelativelysecure. LimitationsofJavaScript: WecannottreatJavaScriptasafull-fledgedprogramminglanguage.Itlackssomeof theimportantfeatureslike: Client-sideJavaScriptdoesnotallowthereadingorwritingoffiles.Thishasbeen keptforsecurityreason. JavaScriptcannotbeusedfornetworkingapplicationsbecausethereisnosuch supportavailable. JavaScriptdoesn'thaveanymultithreadingormultiprocesscapabilities. Ifyourscriptdoesn‟tworkthenyourpageisuseless. Pointstoremember: JavaScriptiscase-sensitive Eachlineofcodeisterminatedbyasemicolon
Variablesaredeclaredusingthekeywordvar Scriptsrequireneitheramainfunctionnoranexitcondition.Therearemajor differencesbetweenscriptsand properprograms.Executionofascriptstarts withthefirstlineofcode&runsuntilthereisnomorecode JavaScriptcomments: InJavaScript,eachlineofcommentisprecededbytwoslashesandcontinuesfrom that pointtotheendoftheline. //thisisjavascriptcomment BlockcommentsorMultilinecomments:/**/ #JavaScriptisnotthesameasJava,whichisabiggerprogramminglanguage (althoughtherearesomesimilarities) JavaScriptandHTMLPage HavingwrittensomeJavaScript,weneedtoincludeitinanHTMLpage.Wecan‟t executethesescriptsfrom acommandline,astheinterpreterispartofthebrowser. Thescriptisincludedin thewebpageandrunbythebrowser,usuallyassoonasthe pagehasbeenloaded.Thebrowserisabletodebugthescriptandcandisplayerrors. EmbeddingJavaScriptinHTMLfile: Ifwearewritingsmallscripts,oronlyuseourscriptsinfewpages,thentheeasiestway istoincludethescriptintheHTMLcode.Thesyntaxisshownbelow:
<scriptlanguage=”javascript”> ……UsingExternalJavaScriptinHTMLfile: Ifweuselotofscripts,orourscriptsarecomplexthenincludingthecodeinsidethe webpagewillmakethesourcefiledifficulttoreadanddebug.Abetterideaistoputthe JavaScriptcodeinaseparatefileandincludethatcodeintheHTMLfile.Byconvention, JavaScriptprogramsarestoredinfileswiththe.jsextension.
<scriptlanguage=”javascript”src=”sample.js”> …… POPUPBOXESINJAVASCRIPT alert(“string”)opensboxcontainingthemessage confirm(“string”)displaysamessageboxwithOKandCANCELbuttons prompt(“string”)displaysapromptwindowwithfieldfortheusertoenteratext stringExample:JavaScriptProgramming Elements
Like anyprogramming language,JavaScripthas variables.A variable is a name assignedtocomputermemorylocationtostoredata.Asthenamesuggests,thevalue ofthevariablecanvary,astheprogram runs.Wecancreateavariablewiththevar statement: var
numberstringbooleanundefinednull.
NumericDataType: Thesearenumbersandcanbeintegers(suchas 2 , 2 2and2 000 )orfloating-point values(suchas 23. 42 ,- 56. 01 ,and2E 45 ). ValidwaystospecifynumbersinJavaScript
Resultof Comparisions
Infinity,-Infinity Numbertoolargeortoo small torepresent
Allinfinityvaluescompare equaltoeachother
NaN UndefinedOperation NaNnevercomparesequalto anything,eventoitself
OperatorsinJavaScript TheoperatorsinJavaScriptcanbeclassifiedasfollows: Arithmeticoperators Relationaloperators Logicaloperators Assignmentoperators
Arithmeticoperators:
Note:Iftheargumentsof+arenumbersthentheyareaddedtogether.Ifthe argumentsarestringsthentheyareconcatenatedandresultisreturned. Example: