Bazaprogram.ru

Новости из мира ПК
4 просмотров
Рейтинг статьи
1 звезда2 звезды3 звезды4 звезды5 звезд
Загрузка...

Math abs javascript

Javascript abs() Function | Math.abs() in Javascript Example

Javascript Math.abs() is an inbuilt function that returns the absolute value of a number. It takes a number as its parameter and returns its absolute value. Parameters: The number whose absolute value is to be found is passed as the parameter to the math.abs() function.

Javascript abs() Function

To find the absolute value of a given argument in JavaScript, Math.abs() method is used. This method is often helpful when the program deals with mathematical expressions where the |x| value (the absolute value of a variable x) is required to be calculated. Since abs() is a static method of Math , it can be used without creating an object.

Syntax

Parameters

The variable x, whose absolute value is to be determined.

Return Value

If the argument is not negative, it returns the value of the argument as it is. Otherwise, it returns the negation of that value.

See the following figure.

  • If the parameter is negative infinity, this method returns positive infinity.
  • If the parameter is a non-numeric string, this method returns NaN.
  • If the parameter is an array with more than one integer, this method returns NaN.
  • If the parameter is an undefined/empty variable, this method returns NaN.
  • If the parameter is an empty object, this method returns NaN.
  • If the parameter is null , this method returns 0.
  • If the parameter is an empty string, this method returns 0.
  • If the parameter is an empty array, this method returns 0.

Compatibility:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari
  • Android webview
  • Chrome for Android
  • Firefox for Android
  • Opera for Android
  • Safari on iOS
  • Samsung Internet
  • Node.js
Читать еще:  Ошибка при запуске приложения 0хс0000022

JavaScript version: ECMAScript 1

Consider the following examples:

The following example demonstrates the use of this method.

Output

Example 2

The following example demonstrates the case where negative infinity is passed as a parameter.

Output

Example 3

The following example demonstrates the cases where NaN is returned.

Output

Example 4

The following example demonstrates the cases where 0 is returned.

Output

Conclusion

If you want to find absolute value in Javascript, then you should use math.abs() function.

See also

Ankit Lathiya is a Master of Computer Application by education and Android and Laravel Developer by profession and one of the authors of this blog.

Ссылка на основную публикацию
Adblock
detector