Number and Math Methods Flashcards
Return boolean check if argument is an integer
Number.isInteger( )
Return a number rounded to a number of decimal places
Why does it return a string?
myNumber.toFixed(numberOfdecimal places )
It returns a string because there is no way to count decimal places in bits (but you can in string indexes). So we have to convert to use .toFixed to convert to string then convert it back to a number.
Returna base 10 number, based off number argument and its number base
Number.parseInt(11, 2)
or just
parseInt()
// expected output
3
Round a number up to the next largest integer
Round a number down up to the next largest integer
Math.ceil(5.5) = 6
Math.floor(5.5) = 5
Return 1 if the argument is positive
Return -1 if the argument is negative
Return 0 if the argument is 0
Math.sign( )
Return the smallest number among the arguments
Return the largest number among the arguments
Do the same but return the max or min element in an array
Math.min() Math.max()
Math.min(…[1, 2]), Math.max(…array)
What does
Math.min()
do?
and/or return?
What happens if an argument is not a number?
The static function Math.min() returns the lowest-valued number passed into it.
Arguemnts are converted to numbers. If any of them are not numbers, it returns NaN.
Math.min()
Math.min(value0)
Math.min(value0, value1)
Math.min(value0, value1, … , valueN)
What does
Math.max()
do?
and/or return?
The Math.max() function returns the largest of the zero or more numbers given as input parameters, or NaN if any parameter isn’t a number and can’t be converted into one.
Math.max()
Math.max(value0)
Math.max(value0, value1)
Math.max(value0, value1, /* … ,*/ valueN)
What does
Math.ceil()
do?
and/or return?
The Math.ceil() function always rounds a number up to the next largest integer.
Math.ceil(x)
What does
Math.floor()
do?
and/or return?
The Math.floor() function returns the largest integer less than or equal to a given number.
Math.floor(x)
What does
Number.prototype.toFixed()
do?
and/or return?
The toFixed() method formats a number using fixed-point notation.
Returns a string with a given number of float points.
toFixed()
toFixed(digits)
What does
Number.isInteger()
do?
and/or return?
The Number.isInteger() method determines whether the passed value is an integer.
Number.isInteger(value)
What does
Number.MAX_VALUE
do?
and/or return?
The Number.MAX_VALUE property represents the maximum numeric value representable in JavaScript.
What does
Number.prototype.toString()
do?
and/or return?
The toString() method returns a string representing the specified Number object.
toString()
toString(radix)
What does
Math.trunc() (not used by internet explorer)
do?
and/or return?
The Math.trunc() function returns the integer part of a number by removing any fractional digits.
Basically it rounds down (unless if it’s got tons of 9s
Math.trunc(x)
Method to remove decimals from a float (basically rounds down to the integer)
Math.trunc() (not used by internet explorer)
Return the max value
Number.MAX_VALUE
What's the difference between: Math.ceil() Math.floor() Math.trunc() Number.prototype.toFixed()
Math.ceil() and Math.floor() round to nearest integer.
Math.trunc() removes floats. It’s like Math.floor() for positive numbers and Math.ceil() for negative numbers.
.toFixed() returns a string and can round to an float you want
Existing method to check if something is NaN (2 methods, what’s the difference?
isNaN() (converts to a number)
Number.isNaN() (does not convert to a number)
Check if a number is finite (not NaN, infinity) (2 ways)
Number.isFinite() (just for numbers)
isFinite() (converts values to numbers first)
What is return from:
isFinite(‘word’)
isFinite(‘’)
False, non numbers are not finite
True (empty and space only strings are converted to 0 when converted to numbers)
What does isFinite() do? (Number.isFinite())
What does inNaN() do? (Number.inNaN())
Method to check if something is NaN
Check if a number is finite (not NaN, infinity)
What does Object.is() do?
special built-in method Object.is that compares values like ===, but is more reliable for two edge cases:
- It works with NaN: Object.is(NaN, NaN) === true, that’s a good thing.
- Values 0 and -0 are different: Object.is(0, -0) === false, technically that’s true, because internally the number has a sign bit that may be different even if all other bits are zeroes.
What is return from:
- Object.is(NaN, NaN)
- NaN == NaN
- Object.is(0, -0)
- 0 == -0
True
False
False
True