Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Advanced web-development with Javascript – Part 2

Monday, October 21st, 2013 by Servage

Data types
The values we assign to variables fall under a few distinct data types.
The simplest of these data types is likely “undefined.” If we declare a variable by giving it a name but no value, that variable contains a value of “undefined.”

var foo;
alert(foo); // This will open a dialog containing "undefined".

Odds are you won’t find a lot of use for this right away, but it’s worth knowing for the sake of troubleshooting some of the errors you’re likely to encounter early on in your JavaScript career. If a variable has a value of “undefined” when it shouldn’t, you may want to double-check that it has been declared correctly or that there isn’t a typo in the variable name.
Similar to the above, assigning a variable of “null” (again, case-sensitive) simply says, “Define this variable, but give it no inherent value.”

var foo = null;

alert(foo); // This will open a dialog containing “null”.
You can assign variables numeric values.

var foo = 5;
alert(foo); // This will open a dialog containing "5".

The word “foo” now means the exact same thing as the number five as far as JavaScript is concerned. Because JavaScript is “loosely typed,” we don’t have to tell our script to treat the variable foo as the number five.

The variable behaves the same as the number itself, so you can do things to it that you would do to any other number using classic mathematical notation: +, -, *, and / for plus, minus, multiply, and divide, respectively. In this example, we use the plus sign (+) to add foo to itself (foo + foo).

var foo = 5;

alert(foo + foo); // This will alert “10”.
Another type of data that can be saved to a variable is a string, which is basically a line of text. Enclosing characters in a set of single or double quotes indicates that it’s a string, as shown here:

var foo = "five";
alert( foo ); // This will alert "five"

The variable foo is now treated exactly the same as the word “five”. This applies to any combination of characters: letters, numbers, spaces, and so on. If the value is wrapped in quotation marks, it will be treated as a string of text. If we were to wrap the number five (5) in quotes and assign it to a variable, that variable wouldn’t behave as a number; instead, it would behave as a string of text containing the character “5.”

Earlier we saw the plus (+) sign used to add numbers. When the plus sign is used with strings, it sticks the strings together (called concatenation) into one long string, as shown in this example.

var foo = "bye"
alert (foo + foo); // This will alert "byebye"

Notice what the alert returns in the following example when we define the value 5 in quotation marks, treating it as a string instead of a number.

var foo = "5";
alert( foo + foo ); // This will alert "55"

If we concatenate a string and a number, JavaScript will assume that the number should be treated as a string as well, since the math would be impossible.

var foo = "five";
var bar = 5;
alert( foo + bar ); // This will alert "five5"
Advanced web-development with Javascript - Part 2, 5.0 out of 5 based on 4 ratings
Categories: Servage


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.