Basics

What's the format in which numbers are stored internally in JavaScript?
All numbers in JavaScript are stored as floatingpoint numbers, using the IEEE754 doubleprecision floatingpoint format. Learn more in JavaScript Numbers â€” Basics: Integers and floats.

What is a number literal?
A number literal is an exact representation of a number in code. For example,
10
is a number literal. Learn more in JavaScript Numbers â€” Basics: Number primitives and objects. 
What is a number primitive and how to create one?
A number primitive is a number without any properties/methods attached to it. Another way to define it is that a number primitive is a number created by a number literal in code. Learn more in JavaScript Numbers â€” Basics: Number primitives and objects.

What is a number object and how to create one?
A number object is a number value with properties/methods attached to it. It is obtained by calling
new Number()
(optionally providingNumber()
an argument). Learn more in JavaScript Numbers â€” Basics: Number primitives and objects. 
Should we manually create number objects in a JavaScript program? Explain your answer.
Definitely not! Number objects are only meant to be created by the JavaScript engine itself, when autoboxing numbers to allow property/method access on them. Learn more in JavaScript Numbers â€” Basics: Number primitives and objects.

What is the purpose of
new Number()
in JavaScript?It creates a number object that has properties/methods (related to numbers) available on it. Learn more in JavaScript Numbers â€” Basics: Number primitives and objects.

What is
Math.floor()
used for?Used to round a given number to the largest integer less than or equal to it. Learn more in JavaScript Numbers â€” Basics:
Math.floor()
. 
What is
Math.ceil()
used for?Used to round a given number to the smallest integer greater than or equal to it. Learn more in JavaScript Numbers â€” Basics:
Math.ceil()
. 
What is the purpose of the
e
symbol in JavaScript when used in a number?The
e
symbol is used to represent a number in scientific notation in JavaScript. Learn more in JavaScript Numbers â€” Basics: Scientific notation. 
What is
Number.MAX_VALUE
?Number.MAX_VALUE
holds the maximum value that can be stored in the IEEE754 doubleprecision floatingpoint format that JavaScript uses internally to store all numbers. Learn more in JavaScript Numbers â€” Basics:Number.MAX_VALUE
. 
List the four ways to parse a number out of a string in JavaScript?
They are as follows:
 The
Number()
function  The
parseInt()
function  The
parseFloat()
function  The unary plus (
+
) operator
Learn more in JavaScript Numbers â€” Basics: Conversion to a number.
 The

What is
NaN
in JavaScript? Is it a number?NaN
is indeed a number. It stands for 'Not a Number' and represents the result of any numeric expression that couldn't be a number or one that couldn't be defined. Learn more in JavaScript Numbers â€” Basics: Special numbers. 
What is meant by the special value
Infinity
?Infinity
is used to denote a mathematically impossible calculation or an expression that exceeds in value beyondNumber.MAX_VALUE
in JavaScript. Learn more in JavaScript Numbers â€” Basics: Special numbers. 
Both
NaN
andInfinity
are keywords in JavaScript. True or false?False. They both are identifiers. To be more specific, both
NaN
andInfinity
are readonly identifiers. Learn more in JavaScript Numbers â€” Basics: Special numbers. 
How to check if a given variable
x
holdsNaN
?Using the
isNaN()
function. Note that this is the only way to check for the valueNaN
. Learn more in JavaScript Numbers â€” Basics: Checking for special numbers. 
How to check if a given variable
x
holdsInfinity
?Using either the
isFinite()
function (and negating its return value using the negation operator (!
)) or by manually comparing againstInfinity
using the identity operator (===
). Learn more in JavaScript Numbers â€” Basics: Checking for special numbers.
Number Methods

Which number method in JavaScript can be used to round a number to a given number of significant figures?
The
toPrecision()
method is used to round a number to a given number of significant figures (and then return the result in the form of a string). Learn more in JavaScript Numbers â€” Number Methods. 
The
toPrecision()
number method rounds a number and then returns back a number as well. True or false?False.
toPrecision()
rounds a number to a given significant figure precision and then returns back a string. This makes perfect sense because representing a number with a fixed s.f. precision is otherwise not possible when working with a number (where5.0
is the same as5.00
,5.000
, and so on).Learn more in JavaScript Numbers â€” Number Methods:
toPrecision()
. 
Why do we need a leave off a space before
.toPrecision()
in the expression100 .toPrecision()
?So as to prevent the expression from producing a syntax error.
When a
.
immediately follows an integer in JavaScript (as in100.
), it signals to the parser that a floatingpoint number is given and that its fractional part follows (containing digits itself). But when the parser reads something else (as in thet
in100.toPrecision()
), it immediately raises a syntax error. The space after the.
(as in100. toPrecision()
) prevents this.Learn more in JavaScript Numbers â€” Number Methods:
toPrecision()
. 
What does
50 .toPrecision()
return?It raises an error because
toPrecision()
requires an argument in the range1
to100
. Learn more in JavaScript Numbers â€” Number Methods:toPrecision()
. 
What does
50 .toPrecision(2)
return?It returns
'50'
, which contains 2 significant figures. 
What does
50 .toPrecision(3)
return?It returns
'50.0'
. 
What does
50 .toPrecision(1)
return? Why is this so?It returns
'5e+1'
, i.e. the number represented in scientific notation. This happens because theprecision
argument provided totoPrecision()
, i.e.1
, is less than the number of digits in the integral part of50
, i.e. 2. Learn more in JavaScript Numbers â€” Number Methods:toPrecision()
. 
What does the
toFixed()
method do?The
toFixed()
number method rounds a number to a given number of decimal places (provided as an argument to the method). Learn more in JavaScript Numbers â€” Number Methods:toFixed()
. 
Give a practical use case of
toFixed()
.toFixed()
is commonly used to represent numbers in applications dealing with money, currency, and finances, or just about any application where a fixed number of decimal places is required before representing a number. Learn more in JavaScript Numbers â€” Number Methods:toFixed()
. 
Why does
toFixed()
return back a string?toFixed()
returns a string because otherwise representing a number in a particular decimalplace precision is impossible when dealing with an actual number value (in the number type). For example, if we log the number5.0000
, we'd just get5
displayed. To get the correct output, we need the number inside a string.In fact, it's not just
toFixed()
that returns a string; the other two methods,toPrecision()
andtoExponential()
, also return strings. Learn more in JavaScript Numbers â€” Number Methods:toFixed()
. 
Why is
toFixed()
called 'toFixed'?toFixed()
is called so because it converts a number to a representation that resembles a fixedpoint number in programming (in contrast to a floatingpoint number, where there is no fixed number of decimal places). Learn more in JavaScript Numbers â€” Number Methods:toFixed()
. 
For an arbitrary number
num
, the return values ofnum.toFixed()
andnum.toFixed(0)
are different. True or false?False; the return values are the same. The single argument of
toFixed()
defaults to0
when omitted; hence,num.toFixed()
is equivalent tonum.toFixed(0)
. Learn more in JavaScript Numbers â€” Number Methods:toFixed()
. 
What does
10 .toFixed(2)
return?'10.00'
. 
What does
10 .toFixed(1)
return?'10.0'
. 
What does the
toExponential()
method do?toExponential()
rounds a number to a given number of decimal places while representing the number in scientific notation. Learn more in JavaScript Numbers â€” Number Methods:toExponential()
. 
For an arbitrary number
num
, the return values ofnum.toExponential()
andnum.toExponential(0)
are the same. True or false?False. Without an argument,
toExponential()
doesn't usually round the fractional part of the provided number. However, with an argument, it rounds the number to that particular decimalplace precision. Learn more in JavaScript Numbers â€” Number Methods:toExponential()
. 
What does
10 .toExponential()
return?'1e+1'
. 
What does
10 .toExponential(2)
return?'1.00e+1'
. 
What does
10 .toExponential(1)
return?'1.0e+1'
. 
What does the
toString()
method do when called on a number, without an argument?It merely converts the number into a string. Learn more in JavaScript Numbers â€” Number Methods:
toString()
. 
What does the argument to
toString()
specify?The radix, also known as the base, to which to convert the given number to. Learn more in JavaScript Numbers â€” Number Methods:
toString()
. 
How to convert the number
50
into each of the following string representations: binary, octal, and hexadecimal?50 .toString(2)
for binary (base 2)50 .toString(8)
for octal (base 8)50 .toString(16)
for hexadecimal (base 16)
Learn more in JavaScript Numbers â€” Number Methods: Converting numbers to different bases.

How to convert a hexadecimal string
hex
into a binary string?Using
parseInt(hex, 16).toString(2)
.First
parseInt(hex, 16)
parses the stringhex
into an integer based on the radix 16. ThentoString(2)
converts this integer into a binary string. Learn more in JavaScript Numbers â€” Number Methods: Converting numbers to different bases.
Math
Object

How to get the constant value ::\pi:: in JavaScript?
Using
Math.PI
. Learn more in JavaScript Numbers â€”Math
Object: Useful Mathematical Constants. 
How to get the value of ::e::, known as Euler's number, in JavaScript?
Using
Math.E
. Learn more in JavaScript Numbers â€”Math
Object: Useful Mathematical Constants. 
What does
Math.round()
do?Math.round()
rounds a number to the nearest integer. Learn more in JavaScript Numbers â€”Math
Object: Rounding to the nearest integer. 
Does
Math.round()
accept any arguments besides the first one?No;
Math.round()
only takes in one argument which is the number to round. 
What does
Math.round(1.4)
return?1
. 
What does
Math.round(1.5)
return?1
. This is because halfway numbers are rounded upwards byMath.round()
. 
What does
Math.round(1.7)
return?2
. 
Math.min()
can accept an array of numbers. True or false?False.
Math.min()
can't accept an array of numbers; it has to be provided the numbers as individual arguments. Learn more in JavaScript Numbers â€”Math
Object:min()
andmax()
. 
Math.max()
can accept an array of numbers. True or false?False.
Math.max()
can't accept an array of numbers; it has to be provided the numbers as individual arguments. Learn more in JavaScript Numbers â€”Math
Object:min()
andmax()
. 
What does
Math.trunc()
do?Math.trunc()
truncates a given number, i.e. removes its fractional part. Learn more in JavaScript Numbers â€”Math
Object: Truncating a number. 
What does
Math.abs()
do?Math.abs()
returns the absolute value of a given number. Learn more in JavaScript Numbers â€”Math
Object. 
What is the purpose of
Math.random()
Math.random()
is used to generate a random number in JavaScript. In particular, it returns back a random number between0
(inclusive) and1
(exclusive). Learn more in JavaScript Numbers â€”Math
Object. 
The
sin()
,cos()
, andtan()
methods ofMath
take the angle values in degrees. True or false?False; they all take the angle values in radians. For example, in order to get the value of ::\text{sin} (90 \degree)::, we'd write
Math.sin(Math.PI / 2)
, sinceMath.PI
(which denotes ::\pi::) represents a ::180 \degree:: angle. Learn more in JavaScript Numbers â€”Math
Object.