HTML and CSS Reference
In-Depth Information
The two additional values numbers can take on are Infinity and NaN . That's right, NaN is a number. (for more
information, see the section “Equality Checking”).
Strings are quite a bit simpler. As in most languages, you can treat a string like an array of characters. However, strings
are also objects, with numerous built-in properties and methods, such as length and slice :
> "example string"[0]
> "example string".length
> "example string".slice(7)
" string"
I should point out that what I'm doing in the previous example is particularly bad. The memory behavior of
where hard-coded strings are allocated isn't part of the language specification. Being allocated on the global heap is
actually one of the better scenarios. Depending on the browser, each individual use could be allocated separately on
the heap, further bloating your memory.
Booleans, as in most languages, can take on the values true and false . Both are reserved keywords in JavaScript. The
main difference here between JavaScript and many other languages lies in which values can be coerced to either true
or false (for further details, see the section “Truthiness,” later in this chapter).
Objects are the bread and butter of JavaScript, but they behave a bit differently from those in several other languages.
In many ways, objects are similar to dictionaries in modern interpreted languages:
x = {};
Curly braces indicate that you're defining an object. Nothing inside suggests that this is the empty object. You can
assign key-value pairs to an object like so:
player = { health: 10 };
Pretty simple, really. You can assign multiple key-value pairs to an object by separating them with a comma:
player = {
health: 10,
position: {
x: 325,
y: 210
Note in this example that you're assigning another object to the position property of player . This is entirely legal
in JavaScript and incredibly simple to do.
Search WWH ::

Custom Search