return 42Press Ctrl+Enter; output:
42
return 2 * 2Output:
4
return x = 256In strict mode, should be:
const x = 256 return x/2Output:
128
return pow(3, 4) + 1 // no need to prepend with MathIn strict mode, should be:
return Math.pow(3, 4) + 1Output:
82
Math.* functions don't need to be prefixed with Math.
Example:
const π = acos(-1)
With strict mode, Math.
should be used:
const π = Math.acos(-1)
Implemented console methods:
console.assert,
console.clear,
console.count,
console.debug,
console.dir,
console.error,
console.info,
console.log,
console.time,
console.timeEnd,
console.timeLog,
console.warn.
The only difference between console.debug, console.dir, console.error, console.info, console.log, and console.warn is the CSS style of a console message. For the objects derived from Object
, these methods show interactive content in the console, representing a hierarchy of the object structure in the form of a tree. The nested levels of the hierarchy are shown when a tree node is open and removed when it is closed. This way, circular references in objects are allowed and represented safely. The second form of these functions, with the first argument representing a format string with substitution represented by the rest of the arguments, is not implemented, as it makes little sense. Instead, using template literals, introduced by ECMAScript 2015, is recommended.
The method console.assert(assertion, ...objects) also behaves like console.log and other similar methods, with the following exception:
the first argument is boolean, reserved for the assert condition. If the first argument assertion
evaluates to true
, nothing happens, otherwise, the assertion failed message is displayed, followed by the rest of arguments ...objects
.
Note:
In some browsers, the timing methods
console.time,
console.timeEnd,
console.timeLog may present accuracy problems. The time reading can be rounded or slightly randomized by a particular browser.
At the moment of writing, correct timing was observed in Blink+V8-based browsers, such as Chromium, Chrome, Opera, and Vivaldi.
Rounding was observed in the browsers using SpiderMonkey.
For further information, please see this documentation page.
Additional console functions:
If one of these functions is called, an additional console view is shown; all output goes to the console, except for the return value which always goes to the text input line at the bottom. In case of exceptions in the code written in the input text area, exception information is written to the console. Whenever possible, the input text area caret is positioned at the point where the problem is. Note that not all browsers support the information on the exception location in the code string.
"use strict";
and this mode is applied to the user code. Please see the check box in the top right corner. Strict mode imposes a number of limitations useful for writing better maintainable, safer code. In strict mode, the prefix Math.
should be used.
See also: strict mode.