Section 1: Definition
A value, the data assigned to a variable, may consist of any sort of data. However, JavaScript considers data to fall into several possible types.
Depending on the type of data, certain operations may or may not be able to be performed on the values.
- For example, you cannot arithmetically multiply two string values.
Data types describe the information that a variable can store, and also describe how many bytes of memory are required to represent a type. Variables can be these types:
Numbers | 3 or 7.987, Integer and floating-point numbers.
|
Booleans |
The possible Boolean values are true and false.
|
Strings |
Strings consist of a series of characters.
|
That said, JavaScript is a loosely typed language – you do not have to specify the data type of a variable when you declare it, and data types are converted automatically as needed during script execution.
- By and large, you may simply assign any type of data to any variable.
- The only time data typing matters is when you need to perform operations on the data. Certain operators behave differently depending on the type of data being dealt with.
Section 2: Numbers
Numbers are the easiest of the data types to understand. They represent numeric values.
integer
The simplest type of number is an integer. An integer is a base-10 whole number. In other words, it is a number with no fractional component. All of the following are valid integers.
- 0
- -75
- 10000000
- 2
JavaScript can handle integer values between 21024 and -21024.
Floating point
Another commonly used type of number is a floating-point number. A floating-point number is a number that is assumed to have a decimal point. Floating-point numbers have a fractional component, even if that fractional component is zero. They can be represented as a real number (integer – decimal point – fractional value) such as in 3.1714, or using exponential notation.
Section 3: Numeric Keyword Literals
JavaScript also has some numeric keyword literals you should know about. These are listed below.
Infinity |
Infinity has the value of infinity, which is to say a number
larger than the largest number that JavaScript can
represent.
|
NaN | NaN is the value returned when you try to treat something that is not a number as a number. For instance the results of 7 times "abc" is not a number. The old form of it is Number.NaN. You can test for not-a-number values with the isNaN() function. |
Number.MAX_VALUE | Number.MAX_VALUE represents the largest representable number for JavaScript. |
Number.MIN_VALUE | Number.MIN_VALUE represents the small representable non-zero number for JavaScript. In other words as close as JavaScript can get to zero with actually reaching zero. |
Section 4: Boolean Values
There are two Boolean values, true and false. These are normally the result of a logical comparison in your code that returns a true/false or yes/no result, such as
a == b
This statement reads: does the value of variable a equal the value of variable b?
When testing for the result of a comparison, JavaScript will treat any non-zero value as true, and a zero value as false. JavaScript will also test to false when you test for the existence of something that does not exist.
You declare Boolean variables like this:
var finished = false;
var passing = true;
Section 5: Strings
A string is sequence of Unicode characters within a given character set. It is normally used to represent text. A string literal is defined by enclosing it in matching single or double quotes.
If you create a string with nothing in it, it is called an empty string. It is normally created by two quote marks with nothing between them.
Some characters that you may want in a string may not exist on the keyboard, or may be special characters that can't appear as themselves in a string. In order to put these characters in a string, you need to use an escape sequence to represent the character. An escape sequence is a character or numeric value representing a character that is preceded by a backslash ( \ ) to indicate that it is a special character.
var home = "c:\\temp";
var quote = "He read \"Ozymandias\" by Percy Bysshe Shelley.";
Some escape characters are as follows:
Escape Sequence | Character | |||
---|---|---|---|---|
\b | Backspace | |||
\t | Tab. Tabs behave erratically on the Web and are best avoided, but sometimes you need them. | |||
\v | Vertical tab | |||
\f | Formfeed | |||
\n | New line (\u0000a). Inserts a line break at the point specified. It is a combination of the carriage return (\r) and the form feed (\f). | |||
\r | Carriage return | |||
\" | Double quote | |||
\' | Single quote, or an apostrophe, such as in can\'t. | |||
\\ | The backslash, since by itself it is a command. | |||
\x99 | A two digit number specifying the hexadecimal value of a character in the Latin-1 character set (ASCII). | |||
\u9999 | A four digit hecadecimal number specifying a character in the Unicode character set. This is not supported before JavaScript 1.3. |
Foreign Characters
To display "foreign" characters in html, use the Unicode code. For
example, to display a ¿ you type "¿". Example: "¿Qué pasa?"
To write foreign characters using JavaScript you use the \u9999 format
specified above, with the 9999 being the hex equivalent of the Unicode
character code. For example, the ¿ has a code of 191 (decimal), which when
converted to Unicode is BF. You must specify four digits, so you write it
as 00BF, or \u00BF. (Converter).
Here is an example
<script>
document.write("\"\u00BFQu\u00E9 pasa?\"");
</script>
Writes
Section 6: Strings and numbers
JavaScript is very relaxed about the difference between strings and numbers. Some programming languages require you to state if a variable is a number or a string before doing anything else with it. Not so in JavaScript. In fact you can even "add up" numbers and strings:
var string3 = string1 + 12;
Some programming languages would shut down in disgust when they encounter this line. After all, string1 is a string (with the value "Hello world!") and 12 is a number. JavaScript, however, tries to solve the problem by assuming 12 is also a string. So string3 becomes
Hello world!12
So if you use + on a string and a number, JavaScript is going to convert the number to a string for you. Better still, if you need to, you can treat numbers as strings or strings as numbers.
Conversely, if you apply mathematics to a string, JavaScript tries to make it a number. If the string cannot be interpreted as a number (because there are letters in it, for instance), JavaScript responds with NaN (Not a Number).
Finally, JavaScript distinguishes no difference between integers and floating point variables.
Number to String
To convert a number to a string, you can do the following:
var temp = (16 * 24)/49 + 12;
string4 = temp.toString();
Now you can use all string methods on string4, while temp still contains the number.
String evaluation
You can evaluate strings that contain numerals, or even equations, using eval. For example,
var str1 = "2+2";
var num = eval(str1); // returns the number 4
The use of eval should be reserved for those very rare instances where there is not any alternative way to produce the required result, because improper use of eval opens up your code for injection attacks.
String to Number
As discussed in the Type Conversion notes, if you want to change a string to a number, first make sure there are only the characters 0-9 in the string. The unary + operator (positive sign) type converts its operand to a number, and because it does not do any additional mathematical operations it is the fastest method for type-converting a string into a number.
var numValue = +stringValue;
For example,
var string6 = '1234';
var newNum = +string6;
Since unary + operator assumes numbers, JavaScript makes the string a number, if possible. If that isn't possible, the result is NaN. Note that you will get unexpected results with
var string6 = '1234';
var newNum = string6 + 0;
This would yield '12340' because JavaScript thinks the + means concatenate string, not add.