Académique Documents
Professionnel Documents
Culture Documents
this.
* block comments doesn't actually need * on line 2 and last-1 but is usually added
for readability.
* simple data types are: undefined, null, boolean, number & string.
- one complex data type: object.
* undefined...
...is mainly used for comparison and should generally not be used as an
assignment.
...is not the same as an undefined variable, which gives a ReferenceError.
* use null as a placeholder for vars that should hold objects in the future,
because null is a empty object pointer.
* you can use Boolean() to convert any value to a boolean. good to check how your
control statements will behave.
* ecmascript always tries to convert numbers to integers because they take up less
space in memory.
- this means that if you type 1. or 1.0 it becomes 1 and not 1.0.
* float numbers can be represented using e-notation., like 3.125e7 which equals to
321250000 or -e for smaller numbers.
* never test for equality using math and floating numbers. 0.1 + 0.2 won't equal
0.3 for instance, beacuse it's only accurate up to 17 decimal places. this is the
same for all languages using the IEEE-754--based numbers.
* on most values, except null & undefined, use toString() to convert to a string.
- to convert null or undefined to a string, use String() because toString()
doesn't exist on those values.
* if primitive value (null, undefined, number, string, bool) you access by value.
you modify the actual stored value.
* generally use objects as argument passing when there's a lot of arguments, and if
a lot of them are optional, else named arguments are easier.
* when using {} or [] the objects constructor isn't called as when you use: new
Object() or new Array()
* Boolean, Number & String's primitive equivalents, i.e. the one you use when
creating variables are not reference types, they are just regular values, but some
behind-the-scenes-magic creates objects, Boolean, Number or String so you can use
the methods of those objects.
When you then type: var s1 = "some text"; s1.substring(2), a String object is
created behind the scenes so you can use a property on a primitive value, which
wouldn't be possible otherwise. It is then immediately destroyed. It's the
equivalent to (basically):
If you use a primitive, the object is immediately destroyed after use, but if you
use the reference type, it acts like a normal object and exists per the normal
rules.
You should generally don't use Boolean, Number or String manually though.