जावास्क्रिप्ट मठ अनुप ()

जावास्क्रिप्ट Math.abs () फ़ंक्शन किसी संख्या का निरपेक्ष मान लौटाता है।

किसी संख्या x का निरपेक्ष मान, जिसे निरूपित किया जाता है | x | , परिभाषित किया जाता है:

  • xयदि x> 0
  • 0यदि x = 0
  • -xयदि x <0

Math.abs()फ़ंक्शन का सिंटैक्स है:

 Math.abs(x)

abs(), एक स्थिर विधि होने के नाते, Mathवर्ग नाम का उपयोग करके कहा जाता है ।

Math.abs () पैरामीटर

Math.abs()समारोह में लेता है:

  • x - वह Numberजिसका पूर्ण मान लौटाया जाना है।

Math.abs से वापसी मूल्य ()

  • निर्दिष्ट संख्या का निरपेक्ष मान लौटाता है।
  • रिटर्न NaNअगर:
    • खाली होना object
    • गैर-सांख्यिक String
    • undefined/ खाली चर
    • Array एक से अधिक तत्वों के साथ
  • 0 रिटर्न अगर:
    • खाली होना String
    • खाली होना Array
    • null

उदाहरण 1: संख्या के साथ Math.abs () का उपयोग करना

 // Using Math.abs() with Number value1 = Math.abs(57); console.log(value1); // 57 value2 = Math.abs(0); console.log(value2); // 0 value3 = Math.abs(-2); console.log(value3); // 2 // Using Math.abs() with numeric non-Number // single item array value = Math.abs((-3)); console.log(value); // 3 // numeric string value = Math.abs("-420"); console.log(value); // 420

आउटपुट

 ५। ० २ ३ ४२०

उदाहरण 2: गैर-संख्या के साथ Math.abs () का उपयोग करना

 // Using Math.abs() with non-Number // Math.abs() gives NaN for // empty object value = Math.abs(()); console.log(value); // NaN // non-numeric string value = Math.abs("Programiz"); console.log(value); // NaN // undefined value = Math.abs(undefined); console.log(value); // NaN // Array with>1 items value = Math.abs((1, 2, 3)); console.log(value); // NaN // Math.abs() gives 0 for // null objects console.log(Math.abs(null)); // 0 // empty string console.log(Math.abs("")); // 0 // empty array console.log(Math.abs(())); // 0

आउटपुट

 ना ना ना ना ना ना ना ० ० ०

दिलचस्प लेख...