Important Notice:

Types of Functions in JavaScript

Types of Functions in JavaScript

22 views 2 min read

Types of Functions in JavaScript JavaScript में Functions के प्रकार

JavaScript में functions को अलग-अलग categories में बाँटा जाता है।
मुख्य रूप से functions के प्रकार:

  1. Math Function
  2. String Function
  3. User Defined Function

1. Math Function

Math Functions ऐसे functions होते हैं जिनका उपयोग mathematical calculations करने के लिए किया जाता है।

JavaScript में Math एक built-in object होता है जिसके अंदर कई useful methods उपलब्ध होते हैं।

इन functions की मदद से हम rounding, square root, power, random number generation आदि calculations आसानी से कर सकते हैं।

Math functions programming को आसान और fast बनाते हैं क्योंकि complex calculations manually करने की जरूरत नहीं पड़ती।

Real Life Use of Math Functions

Math functions का उपयोग कई जगहों पर किया जाता है जैसे:-

  • Calculator applications
  • Games development
  • Billing software
  • OTP generation systems
  • Scientific calculations
  • Banking software

Common Math Functions:-

 

Math.round():-

यह function decimal number को nearest integer में convert करता है।

Example:

 
console.log(Math.round(4.6));
 

Output:

 
5
 

Math.floor():-

यह function decimal value को नीचे वाले integer में बदलता है।

Example:

 
console.log(Math.floor(7.9));
 

Output:

 
7
 

Math.ceil()

यह function decimal value को ऊपर वाले integer में convert करता है।

Example:

 
console.log(Math.ceil(7.1));
 

Output:

 
8
 

Math.sqrt():-

यह function square root निकालता है।

Example:

 
console.log(Math.sqrt(49));
 

Output:

 
7
 

Math.pow():-

यह function power calculate करता है।

Example:

 
console.log(Math.pow(2, 3));
 

Output:

 
8
 

यहाँ 2 की power 3 निकाली गई है।

Math.random():-

यह function random number generate करता है।यह 0 से 1 के बीच random decimal number देता है।

Example:

 
console.log(Math.random());
 

Output हर बार अलग आता है क्योंकि यह random value generate करता है

OTP Generation:-

let otp = Math.floor(1000 + Math.random() * 9000);

 

console.log("OTP:", otp); //output 4 digit random no

Complete flow:-

Math.random()

0.4567

 

* 9000

4110.3

 

+ 1000

5110.3

 

Math.floor()

5110

Complete Example

 
console.log(Math.sqrt(49));
console.log(Math.pow(2, 3));
console.log(Math.round(4.6));
 

Output:

 
7
8
5
 

 

  •  

Related Notes