blob: 62ed641a6bbc34283cdb7959707a3091d68b73f6 (
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
|
---
title: Truthy
slug: Glossaire/Truthy
tags:
- Glossaire
- JavaScript
- Programmation
translation_of: Glossary/Truthy
---
<p>En {{Glossary("JavaScript")}}, une valeur <code>truthy</code> est une valeur qui est considérée comme vraie quand elle est évaluée dans un contexte {{Glossary("Boolean","booléen")}} . Toutes les valeurs sont <code>truthy</code> sauf si elles sont definies comme {{Glossary("Falsy","falsy")}} (c'est-à-dire, sauf pour <code>false</code>, 0, "", <code>null</code>, <code>undefined</code>, et <code>NaN</code>).</p>
<p><br>
{{Glossary("JavaScript")}} utilise la {{Glossary("Type_Conversion", "contrainte")}} de type dans un contexte booléen.</p>
<p><br>
Exemples de valeurs truthy en JavaScript (qui seront considérées comme vraies, ce qui exécutera le bloc if):</p>
<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">if</span> <span class="punctuation token">(</span><span class="keyword token">true</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="punctuation token">{</span><span class="punctuation token">}</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="punctuation token">[</span><span class="punctuation token">]</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="number token">42</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="string token">"foo"</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="keyword token">new</span> <span class="class-name token">Date</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="operator token">-</span><span class="number token">42</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="number token">3.14</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="operator token">-</span><span class="number token">3.14</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="number token">Infinity</span><span class="punctuation token">)</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span><span class="operator token">-</span><span class="number token">Infinity</span><span class="punctuation token">)</span></code></pre>
<h2 id="Voir_aussi">Voir aussi </h2>
<ul>
<li>{{Glossary("Falsy")}}</li>
<li>{{Glossary("Type_Conversion", "Contrainte")}}</li>
<li>{{Glossary("Boolean","Booléen")}}</li>
</ul>
|