This is part 3 of my series, JavaScript Basics. JavaScript has variables, similar to mathematical variables. In the strict sense, they do not behave like mathematical variables. In this part of the series, we look at the meaning of JavaScript variables and some of the type of values that can be assigned to them. The variable name is the name you the web site designer gives.
If you think anything is missing for this article, just contact me at forchatrans@yahoo.com
Example
Consider the following statement;
var myStr = "This is the third part";
Now, var is a JavaScript reserved word. This means that you cannot use it arbitrarily. You use it only when you want to indicate that the next word is a variable. In the above statement, the variable is, myStr, meaning, my String. The value "This is the third part." is in quotes. It is a string. It is assigned to the variable, myStr, using what is called the assignment operator, "=".
Consider now the following two consecutive statements:
var myStr = "This is the third part";
document.write(myStr);
The first statement assigns the value, "This is the third part" to the variable, myStr. The second statement writes (inserts) the value into the document. The second statement has the, write() function. What you put inside the parentheses of a function is called the Argument of the function. What we have as argument here is the, myStr, variable. This variable holds the value, which will be written into the document. There are many situations in computing where you would use the variable instead of the value. Try the following code:
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
var myStr = "This is the third part";
document.write(myStr);
Numbers as Values
You can use numbers as values. You can assign a number to a variable. Consider the following two statements:
var myNum = 56.48;
alert(myNum);
The number, 56.48 is assigned to the variable, myNum. When you are assigning a number, you do not have to put the number in quotes. The second line displays the number in an alert box. The alert function has as argument, myNum. Try the following code:
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
var myNum = 56.48;
alert(myNum);
Assigning a Variable to another Variable
You can assign a variable to another variable. Consider the following two statements:
var str1 = "test";
var str2 = str1;
The first statement assigns the string value, "test" to the variable, str1. The second statement assigns the variable, str1 to str2. The value of str2 is "test" copied from str1.
Changing the Value for a Variable
You can assign a value to a variable and then change it after that. Consider the following statements:
var myStr = "test";
myStr = "good";
The first statement assigns the value, "test" to the variable, myStr. The second statement assigns a new value to the same variable. The final value of, myStr is "good". For the second statement, you do not need the reserved word, var, since you had already used it for the same variable before.
Constant
In the above section, the value, "test" is first assigned to myStr and then "good" is assigned to the same variable. This means the variable myStr is not constant. If you want to prevent a variable from being assigned a new value, you should precede the statement with another reserved word, which is, const; but remove the reserved word, "var". The following code illustrates this. Try it and notice that the value of the variable, myStr is not changed.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
const myStr = "beautiful";
myStr = "handsome";
alert(myStr);
Boolean Variable
In some situations, you can have one of two possible values. The value can either be, true or false. Any variable that deals with these two values is called a Boolean variable. So, you can have something like:
var myVar = true;
or
var myVar = false;
You do not need quotation makes around true or false.
Rule for Naming a Variable
A variable name must start with a letter or underscore, '_'. Within the name, you can have a letter, number or underscore.
Case Sensitivity in JavaScript
JavaScript is said to be case sensitive. This means that for variable names, myVar is not the same as MyVar or myvar or MYVAR, etc.
Declaring a Variable
Before you can use a variable, you have to declare it. To declare a variable, you begin with the reserved word, var and then a space then the name of the variable; just as we have been doing above. You do not have to assign a value to a variable when you declare it. You can do the assignment later. The following code illustrates this:
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
var myVar;
myVar = "you";
alert(myVar);
In your code, for each variable, the reserved word, var is used once. After using, var, if you need the variable name again, you use the name without, var.
Well, var is actually optional. This means that in the declaration of a variable, you can actually omit, var.
When to use Quotation Marks for Values
If your value is a number or a Boolean value, you do not have to use quotation marks; here quotation marks are optional. If your value is a string, you must use quotations marks.
String
A string is a value in quotes.
Integer
An integer is a whole number. It could be a negative number, zero or a positive number.
Floating Point Number
A floating-point number is a number with a decimal point.
Boolean Value
A Boolean value is either true or false.
Literals
When used as a value, a string, integer, floating-point number or Boolean value is called a literal.
We have seen quite a lot. Let us take a break here and continue in the next part of the series.
Chrys
Published by Chrys Forcha
I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a... View profile
- JavaScript Object BasicsThe aim of this article is to quickly make you understand the meaning of Programming Objects (OOP) and how to create them in JavaScript.
- JavaScript Loop StatementsIn JavaScript, you have the do-while loop, the while loop and the for loop. We shall see what all these mean in this article.
- Introduction to JavaScript String Regular ExpressionsIn this article I explain the meaning of Regular Expressions and I introduce you to the concept with the JavaScript String object. You start learning what is called Matching.
- JavaScript ArrayIn JavaScript an array is an object that would hold a list of items. Each item is a literal or a variable representing a literal. In this article I explain the JavaScript Array.
- Comparison and Arithmetic OperatorsIn this part of the series, we talk about some common JavaScript Operators.
- JavaScript Function Basics
- JavaScript Conditional Statements
- Some JavaScript Predefined Objects
- Basics of PHP Variables
- JSON File
- Boolean Logic and JavaScript Conditions
- Some JavaScript Tips
