Difficulty: Easy
Correct Answer: The Math object
Explanation:
Introduction / Context:
JavaScript includes a standard library of built in objects that provide commonly used functionality, including numbers, dates, text processing, and mathematics. For mathematical operations beyond the basic arithmetic operators, JavaScript offers a special Math object. Interview questions about Math constants and functions verify that you know where to find features like PI, square roots, trigonometry, and random number generation.
Given Data / Assumptions:
Concept / Approach:
The Math object in JavaScript is a static object that groups a collection of mathematical constants and functions. It is not a constructor, and you do not create instances of Math. Instead, you access its members directly using syntax like Math.PI or Math.sin(angle). Other objects like Number and Date offer related functionality but do not provide the full set of mathematical operations and constants that the Math object does.
Step-by-Step Solution:
Step 1: Recall that PI in JavaScript is represented as Math.PI, which is a property of the Math object.
Step 2: Functions such as Math.sin(), Math.cos(), Math.tan(), Math.sqrt(), and Math.random() are methods defined on the Math object.
Step 3: The Number object provides properties related to numeric limits and methods for formatting, but it does not offer trigonometric or random functions.
Step 4: The Date object handles dates and times and is unrelated to general mathematics beyond basic timestamp operations.
Step 5: The JSON object is for serialising and parsing JavaScript objects to and from JSON text and has no role in providing mathematical constants.
Verification / Alternative check:
In a JavaScript console, you can test Math constants and methods directly. Typing Math.PI returns the value of pi, approximately 3.14159. Typing Math.random() returns a pseudo random number between 0 and 1. If you try Number.PI or Date.sin(), you will see that these properties and methods do not exist. This confirms that Math is the correct object for these features.
Why Other Options Are Wrong:
Option A is wrong because the Number object provides numeric utility properties like Number.MAX_VALUE, but not trigonometric functions or PI as a standard constant. Option C is incorrect because the Date object is focused on dates and times rather than general mathematics. Option D is wrong because the JSON object is exclusively for working with JSON data structures and does not contain mathematical methods.
Common Pitfalls:
A common mistake is trying to instantiate Math with new Math(), which does not work because Math is not a constructor. Another pitfall is misunderstanding the range of Math.random(); it returns values in the range [0, 1), not including 1, so you need to scale and shift the result to generate random integers in a specific range. Understanding that Math is a static namespace of functions and constants simplifies mathematical operations in JavaScript code.
Final Answer:
The built in object that provides mathematical constants and functions in JavaScript is the Math object.
Discussion & Comments