Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Variables in JavaScript

Variables are used to store information that can be reused in a program. Variables must be given a name that is easy to remember and makes sense. Variables can be upper or lower case and include numbers and underscores, but cannot contain spaces or be JavaScript keywords. Variables are declared using the var keyword followed by the variable name and value. For example, var townName = "Mosman" declares the townName variable and assigns it the string value "Mosman".

Uploaded by

Daniel Wood
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Variables in JavaScript

Variables are used to store information that can be reused in a program. Variables must be given a name that is easy to remember and makes sense. Variables can be upper or lower case and include numbers and underscores, but cannot contain spaces or be JavaScript keywords. Variables are declared using the var keyword followed by the variable name and value. For example, var townName = "Mosman" declares the townName variable and assigns it the string value "Mosman".

Uploaded by

Daniel Wood
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Variables in JavaScript

One of the most useful things in programming is variables. Variables are used to
store information that can be used over and over again. For example, instead of
typing a long number several times it can be stored in a variable called
myNumber and referred to many times in a program. All variables must be given
a name one that is easy to remember and makes sense. Variables like age,
firstName, lastName, and cost are common. The technical name for a variable
name is an identifier.
Variables can be upper or lower case and include underscores. They can also
include numbers but must not begin with a number (eg. 9name is not valid but
name9 would be acceptable). You cannot use spaces in variable names. You also
cant use JavaScript keywords in a variable name, that is, words that are already
used for functions in Python (this will make sense later when you use functions!).
Variables are case-sensitive which means myname1 and myName1 would be
two separate variables. It is good practice to start with lowercase and use an
uppercase letter for the start of every other word eg. myFirstName.
Before you can use a variable anywhere in your code you need to declare it.
This means you are telling JavaScript that this is your variable and you are going
to use it in the program. You declare a variable by using the var keyword, the
variable name, and then assign the variable a value like this:
var [insert variable name] = [insert assigned value]
It is important to note that variables can only store one number or one string. For
storing lists of numbers or strings we would use an array (which we will look at
later).
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>JavaScript Introduction</title>
<script type="text/javascript">
var postcode = 2088;
var townName = Mosman;
document.write(townName);
</script>
</head>
<body>
</body>
</html>

Activity 2
1. What happens if you declare and assign a value (your name) to a variable
called myName and then refer to it as myname later in the program when
using the variable? What error message do you receive in the error
console?
2. Explain the meaning of assignment operator. Which symbol is used as an
assignment operator in JavaScript?
3. Why do you think quotation marks are used around the text value for
townName but not around the number value for postcode?

You might also like