JavaScript Numbers - Questions

Pack 2 52 questions

Things to know

  1. JavaScript Numbers unit required

Basics

  1. What's the format in which numbers are stored internally in JavaScript?

    All numbers in JavaScript are stored as floating-point numbers, using the IEEE-754 double-precision floating-point format. Learn more in JavaScript Numbers — Basics: Integers and floats.

  2. 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.

  3. 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.

  4. 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 providing Number() an argument). Learn more in JavaScript Numbers — Basics: Number primitives and objects.

  5. 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.

  6. 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.

  7. 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().

  8. 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().

  9. 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.

  10. What is Number.MAX_VALUE?

    Number.MAX_VALUE holds the maximum value that can be stored in the IEEE-754 double-precision floating-point format that JavaScript uses internally to store all numbers. Learn more in JavaScript Numbers — Basics: Number.MAX_VALUE.

  11. 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.

  12. 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.

  13. What is meant by the special value Infinity?

    Infinity is used to denote a mathematically impossible calculation or an expression that exceeds in value beyond Number.MAX_VALUE in JavaScript. Learn more in JavaScript Numbers — Basics: Special numbers.

  14. Both NaN and Infinity are keywords in JavaScript. True or false?

    False. They both are identifiers. To be more specific, both NaN and Infinity are read-only identifiers. Learn more in JavaScript Numbers — Basics: Special numbers.

  15. How to check if a given variable x holds NaN?

    Using the isNaN() function. Note that this is the only way to check for the value NaN. Learn more in JavaScript Numbers — Basics: Checking for special numbers.

  16. How to check if a given variable x holds Infinity?

    Using either the isFinite() function (and negating its return value using the negation operator (!)) or by manually comparing against Infinity using the identity operator (===). Learn more in JavaScript Numbers — Basics: Checking for special numbers.

Number Methods

  1. 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.

  2. 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 (where 5.0 is the same as 5.00, 5.000, and so on).

    Learn more in JavaScript Numbers — Number Methods: toPrecision().

  3. Why do we need a leave off a space before .toPrecision() in the expression 100 .toPrecision()?

    So as to prevent the expression from producing a syntax error.

    When a . immediately follows an integer in JavaScript (as in 100.), it signals to the parser that a floating-point number is given and that its fractional part follows (containing digits itself). But when the parser reads something else (as in the t in 100.toPrecision()), it immediately raises a syntax error. The space after the . (as in 100. toPrecision()) prevents this.

    Learn more in JavaScript Numbers — Number Methods: toPrecision().

  4. What does 50 .toPrecision() return?

    It raises an error because toPrecision() requires an argument in the range 1 to 100. Learn more in JavaScript Numbers — Number Methods: toPrecision().

  5. What does 50 .toPrecision(2) return?

    It returns '50', which contains 2 significant figures.

  6. What does 50 .toPrecision(3) return?

    It returns '50.0'.

  7. 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 the precision argument provided to toPrecision(), i.e. 1, is less than the number of digits in the integral part of 50, i.e. 2. Learn more in JavaScript Numbers — Number Methods: toPrecision().

  8. 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().

  9. 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().

  10. Why does toFixed() return back a string?

    toFixed() returns a string because otherwise representing a number in a particular decimal-place precision is impossible when dealing with an actual number value (in the number type). For example, if we log the number 5.0000, we'd just get 5 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() and toExponential(), also return strings. Learn more in JavaScript Numbers — Number Methods: toFixed().

  11. Why is toFixed() called 'toFixed'?

    toFixed() is called so because it converts a number to a representation that resembles a fixed-point number in programming (in contrast to a floating-point number, where there is no fixed number of decimal places). Learn more in JavaScript Numbers — Number Methods: toFixed().

  12. For an arbitrary number num, the return values of num.toFixed() and num.toFixed(0) are different. True or false?

    False; the return values are the same. The single argument of toFixed() defaults to 0 when omitted; hence, num.toFixed() is equivalent to num.toFixed(0). Learn more in JavaScript Numbers — Number Methods: toFixed().

  13. What does 10 .toFixed(2) return?

    '10.00'.

  14. What does 10 .toFixed(1) return?

    '10.0'.

  15. 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().

  16. For an arbitrary number num, the return values of num.toExponential() and num.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 decimal-place precision. Learn more in JavaScript Numbers — Number Methods: toExponential().

  17. What does 10 .toExponential() return?

    '1e+1'.

  18. What does 10 .toExponential(2) return?

    '1.00e+1'.

  19. What does 10 .toExponential(1) return?

    '1.0e+1'.

  20. 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().

  21. 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().

  22. 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.

  23. How to convert a hexadecimal string hex into a binary string?

    Using parseInt(hex, 16).toString(2).

    First parseInt(hex, 16) parses the string hex into an integer based on the radix 16. Then toString(2) converts this integer into a binary string. Learn more in JavaScript Numbers — Number Methods: Converting numbers to different bases.

Math Object

  1. How to get the constant value ::\pi:: in JavaScript?

  2. How to get the value of ::e::, known as Euler's number, in JavaScript?

  3. 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.

  4. Does Math.round() accept any arguments besides the first one?

    No; Math.round() only takes in one argument which is the number to round.

  5. What does Math.round(-1.4) return?

    -1.

  6. What does Math.round(-1.5) return?

    -1. This is because half-way numbers are rounded upwards by Math.round().

  7. What does Math.round(-1.7) return?

    -2.

  8. 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() and max().

  9. 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() and max().

  10. 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.

  11. What does Math.abs() do?

    Math.abs() returns the absolute value of a given number. Learn more in JavaScript Numbers — Math Object.

  12. 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 between 0 (inclusive) and 1 (exclusive). Learn more in JavaScript Numbers — Math Object.

  13. The sin(), cos(), and tan() methods of Math 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), since Math.PI (which denotes ::\pi::) represents a ::180 \degree:: angle. Learn more in JavaScript Numbers — Math Object.