aboutsummaryrefslogtreecommitdiff
path: root/files/tr/web/javascript/reference/global_objects/math/sign/index.html
blob: cbed44a43806151429ce15f68ad45302b4853258 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
title: Math.sign()
slug: Web/JavaScript/Reference/Global_Objects/Math/sign
translation_of: Web/JavaScript/Reference/Global_Objects/Math/sign
---
<div>{{JSRef}}</div>

<div><code><strong>Math.sign()</strong></code> fonksiyonu, parametre olarak aldığı sayının negatif (-1) veya pozitif (1) olduğu bilgisini döndürür. Parametre olarak 0 gönderilirse, +/- 0 döndürür. Sayı pozitifse ayrıca + işareti döndürülmez.</div>

<div></div>

<div>{{EmbedInteractiveExample("pages/js/math-sign.html")}}</div>

<div class="hidden">The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</div>

<h2 id="Syntax">Syntax</h2>

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

<h3 id="Parameters">Parameters</h3>

<dl>
 <dt><code><var>x</var></code></dt>
 <dd>A number. If this argument is not a <code>number</code>, it is implicitly converted to one.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>A number representing the sign of the given argument:</p>

<ul>
 <li>If the argument is positive, returns <code>1</code>.</li>
 <li>If the argument is negative, returns <code>-1</code>.</li>
 <li>If the argument is positive zero, returns <code>0</code>.</li>
 <li>If the argument is negative zero, returns <code>-0</code>.</li>
 <li>Otherwise, {{jsxref("NaN")}} is returned.</li>
</ul>

<h2 id="Description">Description</h2>

<p>Because <code>sign()</code> is a static method of <code>Math</code>, you always use it as <code>Math.sign()</code>, rather than as a method of a <code>Math</code> object you created (<code>Math</code> is not a constructor).</p>

<h2 id="Polyfill">Polyfill</h2>

<pre class="brush: js">if (!Math.sign) {
  Math.sign = function(x) {
    // If x is NaN, the result is NaN.
    // If x is -0, the result is -0.
    // If x is +0, the result is +0.
    // If x is negative and not -0, the result is -1.
    // If x is positive and not +0, the result is +1.
    return ((x &gt; 0) - (x &lt; 0)) || +x;
    // A more aesthetic pseudo-representation:
    //
    // ( (x &gt; 0) ? 1 : 0 )  // if x is positive, then positive one
    //          +           // else (because you can't be both - and +)
    // ( (x &lt; 0) ? -1 : 0 ) // if x is negative, then negative one
    //         ||           // if x is 0, -0, or NaN, or not a number,
    //         +x           // then the result will be x, (or) if x is
    //                      // not a number, then x converts to number
  };
}
</pre>

<p>In the above polyfill, no extra type-coercing is needed to make <code>(x &gt; 0)</code> or <code>(x &lt; 0)</code> numbers because subtracting them from each other forces a type conversion from booleans to numbers.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Using_Math.sign">Using <code>Math.sign()</code></h3>

<pre class="brush: js">Math.sign(3);     //  1
Math.sign(-3);    // -1
Math.sign('-3');  // -1
Math.sign(0);     //  0
Math.sign(-0);    // -0
Math.sign(NaN);   // NaN
Math.sign('foo'); // NaN
Math.sign();      // NaN
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-math.sign', 'Math.sign')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>

<p>{{Compat("javascript.builtins.Math.sign")}}</p>

<h2 id="See_also">See also</h2>

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