--> Sayadasite: Date and math related objects

Multiple Ads

Search

Menu Bar

Date and math related objects

Client side Java script Server side Java script Data types  variables Operators  Expressions  Functions  Objects  Array  Date and math related objects Document objectmodel Event handling

The JavaScript Math object is a built-in, global object that provides properties and methods for performing mathematical operations. 

The JavaScript Math object allows you to perform mathematical tasks.

The Math object is static.

All methods and properties can be used without creating a Math object first.

It is not a constructor function, meaning you cannot create new instances of Math using the new keyword. 

All of its properties and methods are static and can be accessed directly using Math.propertyName or Math.methodName(). 

Key Characteristics:

Static: All properties and methods are static and accessed directly on the Math object (e.g., Math.PI, Math.random()).

Not a constructor: You cannot create Math objects using new Math().

Works with Number type: Math operations are designed for JavaScript's Number type and do not work with BigInt.

Commonly Used Properties:

The Math object in JavaScript provides a clear distinction between its properties and methods, though both are accessed statically.

Definition: 

Properties are named values associated with an object. In the context of the Math object, these are typically mathematical constants.

Purpose: 

They represent fixed, unchangeable values that are commonly used in mathematical calculations.

Characteristics:

They are static, meaning they are accessed directly on the Math object itself (e.g., Math.PI).

They do not perform any actions; they simply provide a value.

Examples

Math.PI: Represents the ratio of a circle's circumference to its diameter, approximately 3.14159.

Math.E: Represents Euler's number, the base of natural logarithms, approximately 2.718.

Math.LN2: Represents the natural logarithm of 2.

Math.LN10: Represents the natural logarithm of 10.

Commonly Used Methods:

Definition: 

Methods are functions associated with an object. In the case of the Math object, these are functions that perform specific mathematical operations.

Purpose: 

They allow you to perform calculations and manipulations on numbers.

Characteristics:

They are static, meaning they are called directly on the Math object (e.g., Math.sqrt(x)).

They perform an action and typically return a result.

Examples

Math.abs(x): Returns the absolute value of x.

Math.ceil(x): Rounds x upwards to the nearest integer.

Math.floor(x): Rounds x downwards to the nearest integer.

Math.round(x): Rounds x to the nearest integer.

Math.trunc(x): Returns the integer part of x by removing any fractional digits.

Math.max(x, y, ...): Returns the largest of zero or more numbers.

Math.min(x, y, ...): Returns the smallest of zero or more numbers.

Math.pow(x, y): Returns x raised to the power of y.

Math.sqrt(x): Returns the square root of x.

Math.random(): Returns a pseudo-random floating-point number between 0 (inclusive) and 1 (exclusive).

Math.sin(x), Math.cos(x), Math.tan(x): Trigonometric functions (input in radians).

Math.log(x): Returns the natural logarithm (base e) of x.

Example:

let radius = 5;
let area = Math.PI * Math.pow(radius, 2); // Calculate the area of a circle
console.log(area); // Output: 78.53981633974483

let randomNumber = Math.random(); // Generate a random number between 0 and 1
console.log(randomNumber);

let roundedNumber = Math.round(4.7); // Round to the nearest integer
console.log(roundedNumber); // Output: 5

No comments: