This article is a review of the JavaScript ES3 parts that, for whatever reason, fade over time in my mind or I outright forget about. Some of the parts mentioned are quirks and some are just plain language realities that I personally find difficult to keep straight.
The eluding parts I discuss here are not focused on method nuances that can be resolved by referring to a JavaScript reference. I have tried to mainly focus on the parts of JavaScript that cannot easily be found in a language reference (i.e the difference between apply()
and call()
).
Additionally, before I begin, I need to clarify the following three points about what I am NOT suggesting by writing this article:
With that said, let's begin.
−0
= +0
= 0
)In JavaScript there is the numeric value -0
and +0
which can indicate from which direction you approached zero. JavaScript, however, attempts to hide this by converting -0
and +0
to 0
.
new
Alone Can Invoke A Constructor FunctionCalling a constructor function with no arguments and the new
keyword alone without the ()
invocation operator will still invoke the constructor function.
instanceof
Does not Work On Primitive ValuesThe instanceof
operator will return false
for the string, number, and boolean primitive values even though JavaScript provides object wrappers for these values. If these values are in their object form (i.e. primitive object wrappers) then instanceof
can be used. Don't forget that null
and undefined
do not have object wrappers.
typeof
Operator Is Limited and BuggyThe typeof
operators usefulness and accuracy is limited to primitive values and contains a bug (i.e. typeof null === "object"
is true
). Because of this, it is commonly avoided and non-native solutions for type checking are best.
true
new Boolean(false)
is not a false
value, because this returns an object wrapper not a primitive false
boolean value. The only false values are NaN
, false
, 0
, null
, undefined
, and ''
.
Arguments are used when a function is being called. We pass arguments to functions. Parameters are set up when the function is defined. So it is said that parameters are used to define a function and arguments are used to invoke a function. These words are typically interchangeable but knowing the distinction can be handy.
If you change the value of a parameter, it does not effect the argument. For instance, if you pass an array argument into a function, then change the value of the parameter, the array argument does not change its reference.
Boolean()
As Function Manually Coverts Any Value To A BooleanCalling the Boolean()
constructor as a function (i.e. without the new
keyword) with an argument value will convert the value to a boolean.
Of course, using the not operator twice has the same effect. It is just not verbose enough for my liking.
new
Returns Primitive ValuesThe primitive string, number, and boolean values can be created by invoking the corresponding constructor function without the new
keyword.
Object()
Can Produce Primitive Object WrappersWhen the Object()
constructor is called as a function and passed a primitive string, number, or boolean value a primitive object wrapper is returned.
While no error occurs when adding properties to primitive values, it is impossible to access these properties. The reason being that the object wrapper, created on the fly, is immediately discarded when access is completed.
delete
Won't Delete Inherited PropertiesThe delete
operator will only work if the object property you are deleting is actually a property (i.e. hasOwnProperty()
=== true
) of the object as opposed to a property inherited from the prototype chain.
Because string wrapper objects create an array-like object (e.g. console.log(new String('foo')); //logs foo {0="f", 1="o", 2="o"}
) we can use an index to access individual characters in a string (fyi >ie7).
The first .
after a primitive number is parsed by JavaScript as a floating point literal. To gain access to object properties on a number wrapper object you can do the following:
Array.length
Can Be Set, And Has Side EffectsBy setting the length
property on an array, one can add undefined
values or remove items from an array.
||
Operator Short-Circuits, Returns First true
ValueThe logical ||
operator will return the first true
value, leaving the remaining values unevaluated (i.e. short-circuiting) because once a true
value is found no other values can change this fact.
&&
Operator Short-Circuits, Returns First false
ValueThe logical &&
operator will return the first false
value, leaving the remaining values un-evaluated (i.e. short-circuiting) because once a false
value is found no other values can change this fact.
null
And When Not To Use undefined
Simply stated, null
is the value for nothing, while undefined
is the lack of a value. It is common practice for a developer to only make use of the null
value when needing to indicate no value as of yet, and leave undefined
for JavaScript to use indicating a total lack of value. By using null
in your program you can differentiate between JavaScript telling you a value is missing and you reminding yourself that you have not yet provided a value.
undefined
By Defaultundefined
represents no value. The following creates no value (i.e. undefined
).
Expressions produce values, while statements perform an action. The clearest picture of this can be found in the different between an if
statement and the ternary operator for creating a conditional expression. The if
statement can't be used anywhere a value can be used, but a conditional expression using the ternary operator can.
++
and --
OperatorsIf the placement of ++
/--
operators comes after a variable (i.e. boo++
) then the value returned is the value previous to incrementing/decrementing. If the placement of ++
/--
operator comes before a variable (i.e. ++foo
) then the value is incremented/decremented and this new value is returned.