Skip to content

Commit 8312468

Browse files
committed
Added console section
1 parent 49299c5 commit 8312468

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

‎chapters/globals.md‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
11

22
# Globals
33

4-
As we have learnt node's module system discourages the use of globals, however node provides a few important globals for use to utilize. The first and most important is the `process` global which exposes process manipulation such as signalling, exiting, the process id (pid), and more.
4+
As we have learnt node's module system discourages the use of globals, however node provides a few important globals for use to utilize. The first and most important is the `process` global which exposes process manipulation such as signalling, exiting, the process id (pid), and more. Other globals help drive to be similar to other familiar JavaScript environments such as the browser, by providing a `console` object.
5+
6+
## console
7+
8+
The `console` object contains several methods which are used to output information to _stdout_ or _stderr_. Lets take a look at what each method does.
9+
10+
### console.log()
11+
12+
The most frequently used console method is `console.log()` simply writing to _stdout_ with a line feed (`\n`). Currently aliased as `console.info()`.
13+
14+
console.log('wahoo');
15+
// => wahoo
16+
17+
console.log({ foo: 'bar' });
18+
// => [object Object]
19+
20+
### console.error()
21+
22+
Identical to `console.log()`, however writes to _stderr_. Aliased as `console.warn()` as well.
23+
24+
console.error('database connection failed');
25+
26+
### console.dir()
27+
28+
Utilizes the _sys_ module's `inspect()` method to pretty-print the object to
29+
_stdout_.
30+
31+
console.dir({ foo: 'bar' });
32+
// => { foo: 'bar' }
33+
34+
### console.assert()
35+
36+
Asserts that the given expression is truthy, or throws an exception.
37+
38+
console.assert(connected, 'Database connection failed');
539

640
## process
741

842
...
43+
File renamed without changes.

0 commit comments

Comments
 (0)