0% found this document useful (0 votes)
14 views5 pages

Javascript - Variables, If..

The document discusses JavaScript variables, if/else statements, and switch statements. It defines variables, shows how to assign values to variables, and describes variable scope and lifetime. It also provides examples of if/else and if/else if/else statements. Finally, it demonstrates how to use switch statements to select code blocks to execute based on different cases.

Uploaded by

papu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Javascript - Variables, If..

The document discusses JavaScript variables, if/else statements, and switch statements. It defines variables, shows how to assign values to variables, and describes variable scope and lifetime. It also provides examples of if/else and if/else if/else statements. Finally, it demonstrates how to use switch statements to select code blocks to execute based on different cases.

Uploaded by

papu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like