Methods
(static) bigint(x) → {bigint}
- Source:
Converts a value into a BigInt.
The argument can be either a number, a boolean, or a string that parses into a number
(the string must wholly parse into a number; it doesn't work like parseInt which will
just stop parsing when it gets to the first non-numeric character).
A number must be an integer; a floating point number will throw a SyntaxError. A number
in scientific notation is fine as long as it represents an integer. (If it does not, such
as in 3.22e1, a RangeError will be thrown.) Keep in mind that numbers that represent
integers larger than Number.MAX_SAFE_INTEGER will be converted but will lose precision.
A string must not be in scientific notation; it will throw a SyntaxError even if the
number is an integer, as in "3e1". A boolean will produce either 1n or 0n.
For the sake of completeness, you can also use an empty array, which produces 0n, or an
array with one element that is a number or a string, which will produce the same number
that the number or string alone would have produced.
Any other argument will throw a SyntaxError (including a single-element boolean array,
for whatever reason).
A string is required to get a number that is greater than Number.MAX_SAFE_INTEGER.
Using a number larger than this (by using scientific notation) will introduce some
inaccuracy.
console.log(bigint (1729)) // 1729n
console.log(bigint (true)) // 1n
console.log(bigint (6.022e23)) // 602200000000000027262976n
console.log(bigint ('602200000000000000000000')) // 602200000000000000000000n
console.log(bigint ([23])) // 23n
Parameters:
| Name | Type | Description |
|---|---|---|
x |
number | string | boolean | array | The value to be converted. |
Throws:
-
-
If the value
xis not a boolean, a number, a string that parses into a number, an empty array, an array of one number, or an array of one string that parses into a number. - Type
- SyntaxError
-
-
-
If a value is a number that is not an integer.
- Type
- RangeError
-
Returns:
A BigInt version of that value.
- Type
- bigint
(static) boolean(x) → {boolean}
- Source:
Converts a value into a boolean.
This uses the same logic as "truthiness" in JavaScript. In other words, if a value is
false, 0, -0, 0n, '', null, undefined, or NaN, it is converted to
false. Every other value is converted to true.
console.log(boolean (0)) // false
console.log(boolean ('')) // false
console.log(boolean (null)) // false
console.log(boolean (14)) // true
console.log(boolean ('test)) // true
console.log(boolean ([])) // true
Parameters:
| Name | Type | Description |
|---|---|---|
x |
* | The value to be converted. |
Returns:
Either true if the value is truthy, or false if it is not.
- Type
- boolean
(static) number(x) → {number}
- Source:
Converts a value into a number.
Any type can be passed to this function, but if it doesn't convert cleanly into a number,
this function will return NaN. null will convert to 0, but undefined will convert
to NaN. Booleans will convert to 1 or 0.
Objects will convert to NaN, but arrays will convert to 0 of they're empty or th
number represented by their element if they have exactly one element (this may still be
NaN). Arrays with more than 1 element convert to NaN.
Strings that parse into numbers and bigints will convert to numbers just fine, but if
they're larger than Number.MAX_SAFE_INTEGER, some precision will be lost. Strings that
represent numbers in scientific (exponential) notation convert fine. Strings that do not
parse into numbers will return NaN.
Only symbols cannot be converted to numbers at all. Trying to do so will throw a
TypeError.
console.log(number ('1729')) // 1729
console.log(number (23n)) // 23
console.log(number (true)) // 1
console.log(number ([])) // 0
console.log(number (['14'])) // 14
console.log(number ({})) // NaN
console.log(number (null)) // 0
console.log(number ()) // NaN
Parameters:
| Name | Type | Description |
|---|---|---|
x |
* | The value to be converted. |
Throws:
-
If the value is a symbol.
- Type
- TypeError
Returns:
The value as a number, or NaN if the value cannot be translated into
a number.
- Type
- number
(static) string(x) → {string}
- Source:
Converts a value to a string.
This function is a thin wrapper over a toString call on the value and thus will return
whatever a regular toString call woudl return. Note that this may or may not make a lot
of sense, depending on what kind of value it is and whether the toString method has
been overridden to make a more sensible string.
console.log(string (1729)) // '1729'
console.log(string (23n)) // '23'
console.log(string (true)) // 'true'
console.log(string ([])) // ''
console.log(string ([14])) // '14'
console.log(string ({})) // '[object Object]'
console.log(string (null)) // 'null'
console.log(string ()) // 'undefined'
Parameters:
| Name | Type | Description |
|---|---|---|
x |
* | The value to be converted. |
Returns:
A string representation of the value.
- Type
- string
(static) symbol(x) → {symbol}
- Source:
Converts a value into a symbol.
The value in question will be converted into a string using its toString method, and it
will be registered in the global Symbol registry under that name. That means if the name
already exists in that registry, that symbol will be returned, rather than a new symbol
created.
Passing no value at all will produce Symbol("undefined").
console.log(symbol ('test')) // Symbol('test')
console.log(symbol (1729)) // Symbol('1729')
console.log(symbol (23n)) // Symbol('23')
console.log(symbol (true)) // Symbol('true')
console.log(symbol ([])) // Symbol('')
console.log(symbol ([14])) // Symbol('14')
console.log(symbol ({})) // Symbol('[object Object]')
console.log(symbol (null)) // Symbol('null')
console.log(symbol ()) // Symbol('undefined')
Parameters:
| Name | Type | Description |
|---|---|---|
x |
* | The value to be converted. |
Returns:
A symbol representing that value. If the value already existed as a symbol, that symbol will be returned; otherwise, a new symbol will be created and returned.
- Type
- symbol