Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Script Lecturer Notes, Study notes of Javascript programming

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

2020/2021

Available from 04/20/2023

laxmiprasanna-muthyala
laxmiprasanna-muthyala 🇮🇳

1 document

Partial preview of the text

Download Java Script Lecturer Notes and more Study notes Javascript programming in PDF only on Docsity!

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

  • Variables,datatypes,operators
  • Statements
  • Arrays
  • Functions
  • ObjectsinJavaScript
  • ExceptionHandling
  • Events
  • DynamicHTMLwithJavaScript

VARIABLES

Like anyprogramming language,JavaScripthas variables.A variable is a name assignedtocomputermemorylocationtostoredata.Asthenamesuggests,thevalue ofthevariablecanvary,astheprogram runs.Wecancreateavariablewiththevar statement: var=; Example: varsum = 0 ;varstr; Wecaninitializeavariablelikethis: str=“hello”; Rulesforvariablenames: #Theymustbeginwithaletter,digitorunderscorecharacter#Wecan‟tusespaces innames#Variablenamesarecasesensitive#Wecan‟tusereservedwordasa variablename. WeaklyTypedLanguage: Mosthigh-levellanguages,includingCandJava,are stronglytyped.Thatis,avariable mustbedeclaredbeforeitisused,anditstypemustbeincludedinitsdeclaration.Once avariableisdeclared,itstypecannotbechanged. AstheJavaScriptis weaklytypedlanguage,datatypesarenotexplicitlydeclared. Example:varnum; num = 3 ; num ="SanDiego"; First,whenthevariable num isdeclared,itisempty.Itsdatatypeisactuallythetype undefined.Thenweassignittothenumber 3 ,soitsdatatypeisnumeric.Nextwe reassignittothestring"SanDiego",sothevariable‟stypeisnowstring. Example:

vars; s=“Hello”; alert( typeof s);s=5 4321 ; alert( typeofs);

DATATYPES

  • JavaScriptsupportsfiveprimitivedatatypes:

numberstringbooleanundefinednull.

  • Thesetypesarereferredtoas primitivetypesbecausetheyarethebasicbuilding blocksfrom whichmorecomplextypescanbebuilt.
  • Ofthefive,onlynumber,string,andbooleanarerealdatatypesinthesenseofactually storingdata.
  • Undefinedandnullaretypesthatariseunderspecialcircumstances.

NumericDataType: Thesearenumbersandcanbeintegers(suchas 2 , 2 2and2 000 )orfloating-point values(suchas 23. 42 ,- 56. 01 ,and2E 45 ). ValidwaystospecifynumbersinJavaScript

  1. 5 - 2. 71. 333333 e 77 - 1. 7 E 123 .E- 5128 e+ 100 Wecanrepresentintegersinoneofthefollowing3ways: Decimal:Theusualnumberswhicharehavingthebase 1 0arethedecimalnumbers Octal:Octalliteralsbeginwithaleadingzero,andtheyconsistofdigitsfrom zero throughseven.Thefollowingareallvalidoctalliterals: 000777024513600 HexaDecimal:Hexadecimalliteralsbeginwithaleading0x,andtheyconsistofdigitsfrom 0through9andlettersAthroughF.Thefollowingareallvalidhexadecimalliterals: 0 x 00 XF 8 f 000 x 1 a 3 C 5 e 7 Special Numeric Values: Specialvalue

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: