I’ve been doing some more reading on JavaScript recently and decided to note down some of what were interesting features for me. If you’ve been doing JavaScript for a while then please let me know if anything looks a bit off, otherwise I hope you find the list an interesting read!
1. There is only one number type in JavaScript
1. There is only one number type in JavaScript
All JavaScript numbers are internally represented as 64 bit floating point values. There is no separate integer type.
We need to take care with floating point arithmetic as it may not always produce the exact answer you were expecting. For example:
2. Blocks in JavaScript do not create a new scope
In some languages (like Java) new variables created within blocks such as if, while and for only exist within that block. JavaScript does not have a block scope.
3. You can create anonymous self executing functions
An anonymous self executing function is just a function that has no name and is executed immediately. It can be useful to induce a block scope and can be useful when dealing with closures within loops.
If you haven’t seen a function like this before, it’s built up like this:
Not very pretty but is an important concept.
4. You can throw objects for exceptions
If you need to throw an exception, rather than just throwing a message string, throw an object that contains a name and a message.
When catching the error you can then test which kind of exception occurred and react accordingly.
5. JavaScript’s try catch has a finally clause
A finally clause in a JavaScript exception will always execute, whether or not an exception was caught. The finally clause will still execute even if there is an error or a break in program flow within the catch clause (such as by a return or break statement).
Testing with no error:
Testing with an error:
6. You can explicitly set an array’s length property
Increasing the length causes any missing elements to have an undefined value. Reducing the length deletes elements beyond that length.
7. There are two equality operators in JavaScript
Javascript has two equality operators; the regular equality operator == and the strict equality operator ===. These have corresponding inequality operators != and !==.
The regular equality operators == and != attempt to convert the operand types when they are not the same type. For example:
The strict equality operators === and !== does not attempt to convert the operand types. If the types are different then they are not the same. For example:
You should generally use the strict equality operator.
For more info see the Comparison operators docs.
8. Falsy, Truthy and boolean values
As you saw in the previous feature, within JavaScript there are several non boolean values that evaluate to false or true. This seems to have led to the terms falsy and truthy.
Falsy values are
- 0
- NaN (not a number)
- ” (empty string)
- false
- null
- undefined
Everything else, including numbers, non empty strings, boolean true, arrays, objects and functions are truthy.
If you have a situation where you must have a real boolean true or false value then you can convert the value using a double negative operator !!.
More details about Falsy and Truthy
9. Parentheses are optional when creating new objects
The parenthesis are optional when creating new objects that require no initial parameters.
10. Hidden JavaScript features
I started compiling this list as a few interesting things I found as I was reading through JavaScript: The Good Parts and doing a bit of research.
No comments:
Post a Comment