<< Chapter < Page Chapter >> Page >

Variable names

Variable names must conform to the naming rules for identifiers. A JavaScript identifier must start with a letter or underscore "_". Following this, you can use either letters or the symbols for the digits (0-9)in the variable name.

JavaScript is case sensitive. Therefore letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

Declaring a variable

In many languages, including JavaScript, you must declare a variable before you can use it. However, JavaScript is very loose in this regard. There are two ways to declare a variable in JavaScript:

  • By simply assigning it a value; for example, x = 10
  • By using the keyword var ; for example, var x = 10

Scope

When working with variables in JavaScript and other languages as well, you must always be concerned about an issue known as scope. Among other things, scope determines which statements in a script have access to a variable.

Two kinds of variables

JavaScript recognizes two kinds of variables:

  • local
  • global

Local variables are variables that are declared inside a function. Global variables are variables that are declared outside a function.

Scope

Local variables may only be accessed by other code within the same function following the declaration of the variable. Hence, the scope of local variables is limited to the function or method inwhich it is declared.

Global variables may be accessed by any code within the script following the declaration of the variable. Hence, the scope of global variables is the entire script.

Use of the keyword var

Using var to declare a global variable is optional. However, you must use var to declare a variable inside a function (a local variable).

A sample script that uses variables

A sample script that uses variables to store data is shown in Listing 4 .

Listing 4 . A sample script that uses variables.
<!-- File JavaScript04.html --><html><body><script language="JavaScript1.3">//Define the function named getArea() function getArea(theRadius) {//declare a local variable var theArea//calculate the area theArea = Math.PI * theRadius * theRadiusreturn theArea }//end function getArea()//=========================================// //declare a global variable without keyword vararea = getArea(3.2)//call the function document.write("Area is: " + area)</script></body></html>

The area of a circle

Listing 4 begins by defining a function that will compute and return the area of a circle given the radius of the circle as an incoming parameter.

The code in the function begins by declaring a variable named theArea . Effectively, this declaration sets aside a pigeon hole in memory and gives it the name theArea . Once the variable is declared, it can be accessed by calling out its name. It can be used to store data, or it can be used to retrievedata previously stored there.

Behavior of the function named getArea

You may recall that the area of a circle is calculated by multiplying the mathematical constant PI by the radius squared. There is no squaringoperator in JavaScript. Therefore, you can square a value by multiplying it by itself.

Questions & Answers

what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
How can I make nanorobot?
Lily
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
how can I make nanorobot?
Lily
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Accessible physics concepts for blind students. OpenStax CNX. Oct 02, 2015 Download for free at https://legacy.cnx.org/content/col11294/1.36
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Accessible physics concepts for blind students' conversation and receive update notifications?

Ask