Types of Functions in JavaScript JavaScript में Functions के प्रकार
JavaScript में functions को अलग-अलग categories में बाँटा जाता है।
मुख्य रूप से functions के प्रकार:
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 का उपयोग कई जगहों पर किया जाता है जैसे:-
Common Math Functions:-
Math.round():-
यह function decimal number को nearest integer में convert करता है।
Example:
Output:
Math.floor():-
यह function decimal value को नीचे वाले integer में बदलता है।
Example:
Output:
Math.ceil()
यह function decimal value को ऊपर वाले integer में convert करता है।
Example:
Output:
Math.sqrt():-
यह function square root निकालता है।
Example:
Output:
Math.pow():-
यह function power calculate करता है।
Example:
Output:
यहाँ 2 की power 3 निकाली गई है।
Math.random():-
यह function random number generate करता है।यह 0 से 1 के बीच random decimal number देता है।
Example:
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
Output: