The JavaScript Diaries: Part 2
The JavaScript Diaries: Part 2
In the first installment, we looked at some general information and guidelines to help prepare us for our study of JavaScript (be sure to check your answers to the review questions). In this section, we'll begin to delve into parts of the language and we'll also write our first script.
Data Types
Scripts manipulate data in order to perform tasks that (hopefully) produce a desired effect. In a nutshell, that's really all there is to JavaScript.
A "data type" is just that: a type of data (e.g., letters, numbers) used within a script. JavaScript is a "loosely typed" language, meaning you don't need to specify the type of data being declared in a variable. The type of data can be changed later in the script without causing an error message (you'll understand this better as we go along). The JavaScript interpreter will determine the data type when it's processed within the script. If you've never done any other programming, don't worry too much about specifying data types. Suffice it to know that, for the most part, the JavaScript interpreter will take care of interpreting the type of data being used.
There are four basic data types used in JavaScript: strings, numbers, booleans, and nulls.
Strings
A string is merely a word or combination of words, including numbers, grouped (strung) together. Any type of character can be stored in a string. While there is no actual limit on the number of characters that a string can hold, keep in mind that many older browsers have a limit of 255 characters. In general, this shouldn't be a problem unless your visitors will be using older browsers. The following are examples of a string:
"cheeseburger" "123 Main Street, Anytown, FL 32714" "His e-mail address is: [email protected]"
A string is enclosed in quotes (" ..... ") to set it apart from the actual code. When the JavaScript interpreter encounters the first quotation mark, it will treat everything after it as a string until it reaches the closing quotation mark. Then it will again begin to interpret everything as code.
Either single or double quotes can be used but you must be consistent. If you start the string with a double quote, it must end with a double quote.
var favfood="a big juicy hamburger"; // Correct var favfood='a big juicy hamburger'; // Correct var favfood="a big juicy hamburger'; // Incorrect
Sometimes you may need to use several sets of quotes in a string. In order to use a double quote within another double quote (or a single within a single) you would need to precede it with a backslash, "\
". This is called an "escape sequence". It would then look like this:
document.write("<a href=\"https://www.domain.com\">Check this out!</a>");
The backslash tells the JavaScript interpreter that the next character should be displayed as it is. You could also combine single and double quotes. (It doesn't matter which set of quotes is used on the outside or the inside. You just need to be consistent.)
document.write("<a href='https://www.domain.com'>Check this out!</a>"); document.write('<a href="https://www.domain.com">Check this out!</a>');
In addition, there are several special characters that can be used in the escape sequence:
Sequence | Name | Sequence | Name |
\b | Backspace | \f | Form feed |
\n | New line | \r | Carriage return |
\t | Tab | \' | Single quote |
\" | Double quote | \\ | Backslash |
\xNN | Latin-1 character set. | \uNNNN | Unicode character set |
These escape sequences are used when you are want to display information on a screen but need a little formatting.
HTML elements can also be used inside of strings. Doing so will help you to create more dynamic pages.
document.write("This is <strong>really</strong> fantastic!");
Be sure to remember that JavaScript strings are case sensitive. "Fender and Gibson"
and "fender and gibson"
are two different strings as far as the JavaScript interpreter sees them. This is especially important when you are comparing strings.
Numbers
JavaScripts recognizes two types of numbers: integers and floating point numbers. Integers are whole numbers (e.g., 1, 20, 546) and floating point numbers are numbers that have a decimal point (e.g., 23.8, 23.890). Unless otherwise specified, JavaScript treats all numbers as floating point numbers, but if a calculation is performed using integers, the answer will be an integer unless the calculation itself changes it (e.g., the number is divided unevenly).
Boolean
A Boolean data type is used for true or false statements. It's mostly used in if()
statements:
var myCar=Chevy; if (myCar==Chevy) { ... task to be performed if statement is true } else { ... task to be performed if statement is false }
We'll look at these types of statements in more detail later but for now, the statement above is executed in the following sequence:
|
This is an excellent method for checking the validity of a statement, such as checking for a valid e-mail address. We'll look at this in more depth when we get into conditional statements.
Null
A null variable has no value. The variable does not return a space or a zero. It just means nothing. A null variable is useful for testing input in scripts. Since it's a keyword, it doesn't need to be enclosed in quotation marks:
var nothingNow=null;
Created: April 29, 2005
URL: