www.crosswinds.net/~fishcaro/day_J4_more_data_types.htm
INDEX CARD #J4:
SPECIAL NUMERIC VALUES (J4a)
When a number is larger than the largest representable positive number, then JavaScript
returns the value as
When a mathematical operation yields an undefined result or an error, then a special
Not-a-Number value, printed as
Here are two special constants:
|
STRINGS (J4b)
So, how can you get a single quote inside a string? Just use double quotes on the outside,
like this:
So, how can you get double quotes inside a string? Just use single quotes on the outside,
like this: Since HTML makes heavy use of double-quoted strings, and since JavaScript is often embedded within HTML strings, it is a good idea to get into the habit of using single quotes around JavaScript strings.
So, how can you get a single quote inside a single-quoted string? Just use the
special JavaScript escape sequence (More on "escape sequences" on the next card.) |
ESCAPE SEQUENCES IN STRINGS (J4c)
Here are some JavaScript escape sequences.
| ||||||||||||||||
WORKING WITH STRINGS (J4d)"a" + "b" produces the string "ab"
To find the length of a string (i.e., the number of characters it contains),
use the
JavaScript strings are indexed starting with zero.
The |
Printable version of Index Card J4a
Printable version of Index Card J4b
Printable version of Index Card J4c
Printable version of Index Card J4d
WORKSHEET #J4:
In Netscape, bring up a JavaScript Interpreter Screen, as discussed in the previous lesson.
Type in each of the following, to explore numbers, strings, and boolean values.
PREDICT what you'll get in each case BEFORE you press enter!
Identify each value of x as either a number or a string.
x = 1/0; document.write("The value of x is " + x + "<BR>");
x = -1/0; document.write("The value of x is " + x + "<BR>");
x = 1.7e308; document.write("The value of x is " + x + "<BR>");
x = 1.8e308; document.write("The value of x is " + x + "<BR>");
x = Math.sqrt(-1); document.write("The value of x is " + x + "<BR>");
x = Infinity+2; document.write("The value of x is " + x + "<BR>");
x = Infinity - Infinity; document.write("The value of x is " + x + "<BR>");
x = Infinity*0; document.write("The value of x is " + x + "<BR>");
x = 5e-324; document.write("The value of x is " + x + "<BR>");
x = 4e-324; document.write("The value of x is " + x + "<BR>");
x = 5e-325; document.write("The value of x is " + x + "<BR>");
x = ''; document.write("The value of x is " + x + "<BR>");
x = typeof(''); document.write("The value of x is " + x + "<BR>");
x = ""; document.write("The value of x is " + x + "<BR>");
x = typeof(""); document.write("The value of x is " + x + "<BR>");
x = 3; document.write("The value of x is " + x + "<BR>");
x = typeof(3); document.write("The value of x is " + x + "<BR>");
x = "3"; document.write("The value of x is " + x + "<BR>");
x = typeof("3"); document.write("The value of x is " + x + "<BR>");
x = "Julia's book"; document.write("The value of x is " + x + "<BR>");
x = 'She said, "Hi!"'; document.write("The value of x is " + x + "<BR>");
x = 'a\bb'; document.write("The value of x is " + x + "<BR>");
x = 'a\fb'; document.write("The value of x is " + x + "<BR>");
x = 'a\nb'; document.write("The value of x is " + x + "<BR>");
x = 'a\rb'; document.write("The value of x is " + x + "<BR>");
x = 'a\tb'; document.write("The value of x is " + x + "<BR>");
x = '\"this quote\"'; document.write("The value of x is " + x + "<BR>");
x = 'a\\b'; document.write("The value of x is " + x + "<BR>");
string="Car"+"ol"; document.write("The value of string is " + string + "<BR>");
x = length.string; document.write("The value of x is " + x + "<BR>");
x = typeof(length.string); document.write("The value of x is " + x + "<BR>");
Infinity
be used in arithmetic operations?
<A HREF="" onClick="alert('Thank you!')">Please click here.</A>
<A HREF="" onClick="alert("Thank you!")">Please click here.</A>
<SCRIPT LANGUAGE="JavaScript">
string1 = "abc";
string2 = "def";
string = string1 + string2;
document.write("string = " + string + "<BR>");
length_of_string = string.length;
document.write("length_of_string = " + length_of_string + "<BR>");
firstchar = string.charAt(0);
document.write("first character is " + firstchar + "<BR>");
secondchar = string.charAt(1);
document.write("second character is " + secondchar + "<BR>");
</SCRIPT>