aboutsummaryrefslogtreecommitdiff
path: root/files/pl/web/javascript/reference/global_objects/math/abs/index.html
blob: 7130c60e4d3c771ec6f82a21824c4ba79063c309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---
title: Math.abs()
slug: Web/JavaScript/Reference/Global_Objects/Math/abs
tags:
  - JavaScript
  - Math
  - Method
translation_of: Web/JavaScript/Reference/Global_Objects/Math/abs
original_slug: Web/JavaScript/Referencje/Obiekty/Math/abs
---
<p>{{JSRef}}</p>

<h2 id="Podsumowanie" name="Podsumowanie">Podsumowanie</h2>

<p>Zwraca wartość bezwzględną danej liczby.</p>

<p><math display="block"><semantics><mrow><mstyle mathvariant="monospace"><mrow><mo lspace="0em" rspace="thinmathspace">Math.abs</mo><mo stretchy="false">(</mo><mi>x</mi><mo stretchy="false">)</mo></mrow></mstyle><mo>=</mo><mrow><mo stretchy="false">|</mo><mi>x</mi><mo stretchy="false">|</mo></mrow><mo>=</mo><mrow><mo>{</mo><mtable columnalign="left left"><mtr><mtd><mi>x</mi></mtd><mtd><mtext>if</mtext><mspace width="1em"></mspace><mi>x</mi><mo>&gt;</mo><mn>0</mn></mtd></mtr><mtr><mtd><mi>0</mi></mtd><mtd><mtext>if</mtext><mspace width="1em"></mspace><mi>x</mi><mo>=</mo><mn>0</mn></mtd></mtr><mtr><mtd><mo>-</mo><mi>x</mi></mtd><mtd><mtext>if</mtext><mspace width="1em"></mspace><mi>x</mi><mo>&lt;</mo><mn>0</mn></mtd></mtr></mtable></mrow></mrow><annotation encoding="TeX">{\mathtt{\operatorname{Math.abs}(x)}} = {|x|} = \begin{cases} x &amp; \text{if} \quad x \geq 0 \\ -x &amp; \text{if} \quad x &lt; 0 \end{cases} </annotation></semantics></math></p>

<h2 id="Sk.C5.82adnia" name="Sk.C5.82adnia">Składnia</h2>

<pre class="syntaxbox"><code>Math.abs(<var>x</var>)</code></pre>

<h3 id="Parametry" name="Parametry">Parametry</h3>

<dl>
 <dt><code>x</code> </dt>
 <dd>liczba.</dd>
</dl>

<h2 id="Opis" name="Opis">Opis</h2>

<p>Ponieważ <code>abs</code> jest statyczną metodą obiektu {{jsxref("Math")}}, zawsze odwołujemy się do niej poprzez <code>Math.abs()</code>, a nie jak do metody instancji obiektu {{jsxref("Math")}} utworzonej przez użytkownika.</p>

<h2 id="Przyk.C5.82ady" name="Przyk.C5.82ady">Przykłady</h2>

<h3 id="Przyk.C5.82ad:_Zastosowanie_Math.abs" name="Przyk.C5.82ad:_Zastosowanie_Math.abs">Przykład: Zastosowanie <code>Math.abs()</code></h3>

<p>Poniższa funkcja zwraca wartość bezwzględną zmiennej <code>x</code>:</p>

<pre class="brush: js">Math.abs('-1'); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs('string'); // NaN
Math.abs(); // NaN
</pre>

<h2 id="Zobacz_tak.C5.BCe" name="Zobacz_tak.C5.BCe">Zobacz także</h2>

<ul>
 <li>{{jsxref("Math.ceil()")}}</li>
 <li>{{jsxref("Math.floor()")}}</li>
 <li>{{jsxref("Math.round()")}}</li>
 <li>{{jsxref("Math.sign()")}} {{experimental_inline}}</li>
 <li>{{jsxref("Math.trunc()")}} {{experimental_inline}}</li>
</ul>