The Power of Javascript: Basic Types of Data - Data as Numbers (Page 2 of 4 )
Numbers such as 12 or -12 are called integer numbers because they're whole numbers. Numbers such as 12.34 or -12.34 are called floating point numbers because these numbers contain a decimal point and a fraction value. In Javascript we have one data type called number that stores both the integer and floating point number values. Actually, Javascript stores all the numerical values as floating point values, so if you stored the value 1.00 it means 1 to Javascript (as an integer value) and when you add a fraction, for example 1.12, Javascript understands that it's a floating point value. The range of values that can be used for the number data type is very big, from ±5.0 × 10−324 to ±1.7 × 10308 for floating point numbers, and for integer numbers from –253 to 253. We will not need this range of values unless we are working on scientific applications.
Data as Character Strings
Like most programming languages, Javascript provides a data type that stores text as character strings and not as code. It's called a string because it's a row of characters. To define a string value you use the double quotation marks to delimit the string value like "Javascript is fun". You can use the single quotation marks instead of the double marks, so you could write it as 'Javascript is fun'. Note that you must match the opening and closing quotation marks so you can't write a string value like 'Javascript is fun" or "Javascript is fun'.
When the Javascript interpreter encounters a value between double quotes (or single quotes) it deals with it as text, not code, so the value "34.43" is a string, not a number, and the value "var x = 34" is also a string, not Javascript code. Usually, you use string values to display messages to users and to gather user information and validate it along with other operations that we will be doing throughout the series. Suppose that you need to store the following value as a string:
I'm a Javascript Programmer
If you used the single quotes to delimit a string value, you would have something like this:
'I'm a Javascript Programmer'
This line produces an error because the interpreter thinks that the apostrophe in I'm is the closing single quote of the string, so to the interpreter the string consists of 'I' only. To solve this problem, use double quotes to delimit the string, so it will be "I'm a Javascript Programmer". The same rule applies if you want to include a double quote inside the string value. You just have to use the single quotes to delimit string value that contains a double quote inside. We will look at an example after a while about all the data types.
A string value must be written in one line. It can't span two or more lines, so you can't write a string value like this:
"Hello guys,
I'm learning Javascript"
This produces an error. If you want to include a new line in your string value, you can use the escape character \n. There are many characters that can't be represented directly in a string value; for example, the copyright symbol and the new line character. Can you type these characters directly in your string value? No, and that's why most programming languages have the concept of escape characters, which are a special value that has a special meaning when used.
An escape character consists of a backslash followed by a character. It is called an escape character because it doesn't appear (escape) in the string as you write it; instead it has a special meaning and functionality. For example, to include a new line inside your string value use the new line (\n) escape character, to include the copyright symbol in your string value use the Escape Character \xA9. Suppose that you have the string value "Hello\nGuys". When the Javascript interpreter encounters this value, it knows that it's a string value because of the double quotes around it. Then the interpreter searches for an escape character, so Hello is a string value, then it finds \n which means escape the value \n, put in a new line character, and then continue the string value. We will see an example covering escape characters shortly. The following are the available Escape Characters
| \' | Single Quote Escape Character |
| \" | Double Quote Escape Character |
| \n | New Line Escape Character |
| \t | Tab Escape Character |
| \r | Carriage Return |
| \f | Form Feed |
| \e | Backspace |
| \b | Escape |
| \\ | Backslash |
You can use the form \ddd to include a character with the Latin-1 Encoding using the Octal Base. You can also use the form \xdd to include a character with the Latin-1 Encoding, but this time using the Hexadecimal representation, where dd is the character code. Or you can use the form \xdddd to include a Unicode Character using the Hexadecimal representation, where dddd is the character code.
Next: Boolean Data, Yes or No >>
More JavaScript Articles
More By Michael Youssef