Converting from one type to another frequently causes bugs or unexpected result if you’re not careful. For example, when converting a string to an integer, we use the following function:
1
var num = parseInt(string, radix);
Accidentally, I used an array of numeric strings in place of string for conversion.
One of the benefits coding in JavaScript is the ability to share code between browser (front-end) and Node (back-end) environments.
To share code between two different environment, first need to understand what are specific to browser and what are specific to Node. Shared code should be agnostic to both environments.
In browser environment, there is an obvious one: window object. In Node environment, there are a few objects that are not globally defined in browser: global, process, module, exports and more, see global objects. All these variables work, but exports is preferred.
This is a Setext-style headers, which are ‘underlined’ using equal signs (for first-level headers) and dashes (for second-level headers). In order to generate those underlined equal signs, a simple loop could be used to repeat the character = several times. By the way, it is not necessary to have exact number of the equal signs as the header length, but it just looks better. But there is another way:
1
Array(heading.length + 1).join('=')
It is using the JavaScript Array global object to construct an empty Array with the specified array length. Clever indeed!