The lack of transitivity is alarming. My advice is to never use the evil twins. Instead, always use === and !== . All of the comparisons just shown produce false with the === operator.
It is recommended that you always use JSON.parse instead of eval to defend against server incompetence. It is also good practice for the day when the
browser provides safe data access to other servers.
When you evaluate a null variable, the null value behaves as 0 in numeric contexts and as false in boolean contexts. For example:
let vs var
Property names that are not valid identifiers also cannot be accessed as a dot (.) property, but can be accessed and set with the array-like notation(“”).
If the finally block returns a value, this value becomes the return value of the entire try-catch-finally production, regardless of any return statements in the try and catch blocks.
A simple example using Promise and XMLHttpRequest
The following function takes as its argument an object and the object’s name. It then iterates over all the object’s properties and returns a string that lists the property names and their values. dump_props
A function that calls itself is called a recursive function. In some ways, recursion is analogous to a loop. Both execute the same code multiple times, and both require a condition (to avoid an infinite loop, or rather, infinite recursion in this case).
It is possible to convert any recursive algorithm to a non-recursive one, but often the logic is much more complex and doing so requires the use of a stack. In fact, recursion itself uses a stack: the function stack.
You can nest a function within a function. The nested (inner) function is private to its containing (outer) function. It also forms a closure.
A closure is an expression (typically a function) that can have free variables together with an environment that binds those variables (that “closes” the expression).
Using the arguments object
The arguments of a function are maintained in an array-like object. Within a function, you can address the arguments passed to it as follows:
Until arrow functions, every new function defined its own this value (a new object in case of a constructor, undefined in strict mode function calls, the context object if the function is called as an “object method”, etc.). This proved to be annoying with an object-oriented style of programming.
Alternatively, a bound function could be created so that the proper this value would be passed to the growUp() function.
Arrow functions capture the this value of the enclosing context, so the following code works as expected.
Bitwise logical operators
The delete operator deletes an object, an object’s property, or an element at a specified index in an array. The syntax is:
array, delete vs undefined
The in operator returns true if the specified property is in the specified object.
Use instanceof when you need to confirm the type of an object at runtime. For example, when catching exceptions, you can branch to different exception-handling code depending on the type of exception thrown.
In the following example, the function JSClock() returns the time in the format of a digital clock.
Using a regular expression literal, which consists of a pattern enclosed between slashes, as follows:
Methods that use regular expressions
When you want to know whether a pattern is found in a string, use the test or search method; for more information (but slower execution) use the exec or
Advanced searching with flags
Regular expressions have four optional flags that allow for global and case insensitive searching. These flags can be used separately or together in any
order, and are included as part of the regular expression.
because normal elements and all enumerable properties will be listed.
array and sort
map(callback[, thisObject]) returns a new array of the return value from executing callback on every array item.
filter(callback[, thisObject]) returns a new array containing the items for which callback returned true.
reduce(callback[, initialValue]) applies callback(firstValue, secondValue) to reduce the list of items down to a single value.
ECMAScript 6 introduces a new data structure to map values to values. A Map object is a simple key/value map and can iterate its elements in insertion
Use maps over objects when keys are unknown until run time, and when all keys are the same type and all values are the same type.
Use objects when there is logic that operates on individual elements.
One use case of WeakMap objects is to store private data for an object or tohide implementation details.
Set objects are collections of values. You can iterate its elements in insertion order.
A value in a Set may only occur once; it is unique in the Set’s collection.
WeakSet objects are collections of objects. An object in the WeakSet may only occur once; it is unique in the WeakSet’s collection and objects are not
enumerable. The use cases of WeakSet objects are limited. They will not leak memory so it can be safe to use DOM elements as a key and mark them
for tracking purposes.
Before ECMAScript 5, there was no native way to list all properties of an object. However, this can be achieved with the following function:
Using the Object.create method
The following illustrates how getters and setters could work for a userdefined object o.
prototypical object, an object used as a template from which to get the initial properties for a new object. Any object can specify its own
properties, either when you create it or at run time. In addition, any object can be associated as the prototype for another object, allowing the second
object to share the first object’s properties.
You can use call to chain constructors for an object, similar to Java.