blob: b24b0c938d5157eaa5dd762acd65f47cd9ef2236 (
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
|
---
title: Booléen
slug: Glossary/Boolean
tags:
- Booléen
- Glossaire
- JavaScript
- Langages de programmation
- Types de données
translation_of: Glossary/Boolean
original_slug: Glossaire/Boolean
---
<p>En informatique, un <strong>booléen</strong> est un type de données logique qui ne peut prendre que deux valeurs : <code>true</code><strong> </strong><em>(vrai)</em><strong> </strong>ou <code>false</code><em> (faux)</em>. Par exemple, en JavaScript, les conditions booléennes sont souvent ouvertes pour décider quelle section de code doit être exécutée (comme dans l'<a href="https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/if...else">instruction If</a>) ou répétée (comme pour une <a href="https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Instructions/for">boucle For</a>).</p>
<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="comment token">/* JavaScript instruction if */</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span>boolean conditional<span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="comment token">// code à exécuter si la condition est true <em>(vrai)</em></span>
<span class="punctuation token">}</span>
<span class="keyword token">if</span> <span class="punctuation token">(</span>boolean conditional<span class="punctuation token">)</span> <span class="punctuation token">{</span>
console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span><span class="string token">"boolean conditional resolved to true"</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span> <span class="keyword token">else</span> <span class="punctuation token">{</span>
console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span><span class="string token">"boolean conditional resolved to false"</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span>
<span class="comment token">/* JavaScript boucle for */</span>
<span class="keyword token">for</span> <span class="punctuation token">(</span>control variable<span class="punctuation token">;</span> boolean conditional<span class="punctuation token">;</span> counter<span class="punctuation token">)</span> <span class="punctuation token">{</span>
<span class="comment token">// </span></code> <span id="result_box" lang="fr"><span>code à exécuter répétitivement si la condition est vraie</span></span> <code class="language-js">
<span class="punctuation token">}</span>
<span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> i<span class="operator token">=</span><span class="number token">0</span><span class="punctuation token">;</span> i <span class="operator token"><</span> <span class="number token">4</span><span class="punctuation token">;</span> i<span class="operator token">++</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span><span class="string token">"I print only when the boolean conditional is true"</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span></code></pre>
<h2 id="Pour_Approfondir">Pour Approfondir</h2>
<h3 id="Culture_générale">Culture générale</h3>
<ul>
<li><a href="http://fr.wikipedia.org/wiki/Bool%C3%A9en">Booléen</a> sur Wikipedia</li>
</ul>
<h3 id="Informations_techniques">Informations techniques</h3>
<ul>
<li>L'objet JavaScript global : {{jsxref("Boolean")}}</li>
<li><a href="https://developer.mozilla.org/fr/docs/Web/JavaScript/Structures_de_donn%C3%A9es">Structures de données JavaScript</a> sur MDN</li>
</ul>
|