Vous êtes sur la page 1sur 2

* everything camelCase because the built in functions and objects are written like

this.

* block comments doesn't actually need * on line 2 and last-1 but is usually added
for readability.

* variables default to undefined if not initialized.

* it's not recommended to change the data type of a variable.

* 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.

* if reference value, i.e. objects, (like a pointer/reference in other languages)


you access them by reference. you don't modify the memory directly, similar to
perl.
- practically, you only have to worry about objects, think of them as
references in perl.

* 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.

* use dot notation whenever, and only bracket when needed

* when using {} or [] the objects constructor isn't called as when you use: new
Object() or new Array()

* use Array.isArray() to check if a variable is an array

* use spread syntax (...iterable) to expand a full array like:


myFunction(...iterableObj);
[...iterableObj, 4, 5, 6];
let objClone = { ...obj };

* 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):

var s1 = new String("some text"); s1.substring(2); s1 = null;

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.

Vous aimerez peut-être aussi