JSVariables,If...
Else,Switch
Note:manyofthecontentsofthispagearetakenfromw3schoolwebsite.
Variables
[Link]:
<scripttype="text/javascript">
varname="Hege"
[Link](name)
[Link]("<h1>"+name+"</h1>")
</script>
Avariableisa"container"[Link]'svaluecanchangeduringthe
[Link].
Rulesforvariablenames:
Variablenamesarecasesensitive
Theymustbeginwithaletterortheunderscorecharacter
DeclareaVariable
Youcancreateavariablewiththevarstatement:
varstrname=somevalue
Youcanalsocreateavariablewithoutthevarstatement:
strname=somevalue
AssignaValuetoaVariable
Youcanassignavaluetoavariablelikethis:
varstrname="Hege"
Orlikethis:
strname="Hege"
Thevariablenameisontheleftsideoftheexpressionandthevalueyouwanttoassigntothevariableis
[Link]"strname"hasthevalue"Hege".
LifetimeofVariables
Whenyoudeclareavariablewithinafunction,thevariablecanonlybeaccessedwithinthatfunction.
Whenyouexitthefunction,[Link]
havelocalvariableswiththesamenameindifferentfunctions,becauseeachisrecognizedonlybythe
functioninwhichitisdeclared.
Ifyoudeclareavariableoutsideafunction,[Link]
thesevariablesstartswhentheyaredeclared,andendswhenthepageisclosed.
If...Else
Thefollowingisanexampleofusingifstatement:
<scripttype="text/javascript">
vard=newDate()
vartime=[Link]()
if(time<10)
{
[Link]("<strong>Goodmorning</strong>")
}
</script>
ConditionalStatements
Veryoftenwhenyouwritecode,[Link]
useconditionalstatementsinyourcodetodothis.
InJavaScriptwehavethefollowingconditionalstatements:
ifstatementusethisstatementifyouwanttoexecutesomecodeonlyifaspecifiedconditionis
true
if...elsestatementusethisstatementifyouwanttoexecutesomecodeiftheconditionistrueand
anothercodeiftheconditionisfalse
if...elseif....elsestatementusethisstatementifyouwanttoselectoneofmanyblocksofcodeto
beexecuted
switchstatementusethisstatementifyouwanttoselectoneofmanyblocksofcodetobe
executed
IfStatement
Youshouldusetheifstatementifyouwanttoexecutesomecodeonlyifaspecifiedconditionistrue.
Syntax:
if(condition)
{
codetobeexecutedifconditionistrue
}
[Link](IF)willgenerateaJavaScripterror!
<scripttype="text/javascript">
//Write"Lunchtime!"ifthetimeis11
vard=newDate()
vartime=[Link]()
if(time==11)
{[Link]("<em>Lunchtime!</em>")}
</script>
Note:Whencomparingvariablesyoumustalwaysusetwoequalssignsnexttoeachother(==)!
If...elseStatement
Ifyouwanttoexecutesomecodeifaconditionistrueandanothercodeiftheconditionisnottrue,use
theif....elsestatement.
Syntax:
if(condition)
{
codetobeexecutedifconditionistrue
}
else
{
codetobeexecutedifconditionisnottrue
}
Example
<scripttype="text/javascript">
//Ifthetimeislessthan10,
//youwillgeta"Goodmorning"greeting.
//Otherwiseyouwillgeta"Goodday"greeting.
vard=newDate()
vartime=[Link]()
if(time<10)
{
[Link]("Goodmorning!")
}
else
{
[Link]("Goodday!")
}
</script>
If...elseif...elseStatement
Youshouldusetheif....elseif...elsestatementifyouwanttoselectoneofmanysetsoflinestoexecute.
Syntax:
if(condition1)
{
codetobeexecutedifcondition1istrue
}
elseif(condition2)
{
codetobeexecutedifcondition2istrue
}
else
{
codetobeexecutedifcondition1and
condition2arenottrue
}
Example
<scripttype="text/javascript">
vard=newDate()
vartime=[Link]()
if(time<10)
{
[Link]("<b>Goodmorning</b>")
}
elseif(time>10&&time<16)
{
[Link]("<b>Goodday</b>")
}
else
{
[Link]("<b>HelloWorld!</b>")
}
</script>
Switch
Youshouldusetheswitchstatementifyouwanttoselectoneofmanyblocksofcodetobeexecuted.
Syntax:
switch(n)
{
case1:
executecodeblock1
break
case2:
executecodeblock2
break
default:
codetobeexecutedifnis
differentfromcase1and2
}
Thisishowitworks:Firstwehaveasingleexpressionn(mostoftenavariable),thatisevaluatedonce.
[Link]
match,[Link]
runningintothenextcaseautomatically.
Example
<scripttype="text/javascript">
//Youwillreceiveadifferentgreetingbased
//[Link]=0,
//Monday=1,Tuesday=2,etc.
vard=newDate()
theDay=[Link]()
switch(theDay)
{
case5:
[Link]("FinallyFriday")
break
case6:
[Link]("SuperSaturday")
break
case0:
[Link]("SleepySunday")
break
default:
[Link]("I'mlookingforwardtothisweekend!")
}
</script>
2007MehmudAbliz