Logica JavaScript
- Pagina precedente JS casuale
- Pagina successiva Confronto JS
JavaScript booleans (logical) represent one of two values:true
or false
.
boolean values
In programming, you often need a data type that can only have one of two values, such as
- YES / NO
- ON / OFF
- TRUE / FALSE
For this reason, JavaScript provides abooleandata type. It only accepts values true or false.
Boolean() function
You can use Boolean()
Function to determine if an expression (or variable) is true:
Esempio
Boolean(10 > 9) // returns true
Or even simpler:
Esempio
(10 > 9) // also returns true 10 > 9 // also returns true
Comparisons and Conditions
This chapter of JS Comparisons lists the complete comparison operators.
This chapter of JS Conditions lists the complete conditional statements.
Here are some examples:
operator | description | Esempio |
---|---|---|
== | equal to | if (day == "Monday") |
> | greater than | if (salary > 9000) |
< | less than | if (age < 18) |
The boolean value of expressions is the basis for JavaScript comparisons and conditions.
All values that have a 'truthy' value are True
Esempio
100 3.14 -15 "Hello" "false" 7 + 1 + 3.14 5 < 6
All values that do not have a 'truthy' value are False
0 (zero)The boolean value of false:
var x = 0; Boolean(x); // returns false
-0 (negative zero)The boolean value of false:
var x = -0; Boolean(x); // returns false
"" (empty value)The boolean value of false:
var x = ""; Boolean(x); // returns false
undefined The boolean value of false:
var x; Boolean(x); // returns false
null The boolean value of false:
var x = null; Boolean(x); // returns false
false The boolean value (as you might have guessed) is false:
var x = false; Boolean(x); // returns false
NaN The boolean value of false:
var x = 10 / "H"; Boolean(x); // returns false
Booleans can also be objects
In JavaScript, booleans are usually created from literals as primitive values:
var x = false
But boolean can also be defined by the keyword new
Defined as an object:
var y = new Boolean(false)
Esempio
var x = false; var y = new Boolean(false); // typeof x returns boolean // typeof y returns object
Non creare oggetti booleani. Può rallentare l'esecuzione.
new
La parola chiave può complicare il codice e produrre risultati inaspettati:
Quando si utilizza l'operatore ==
L'operatore
Esempio
var x = false; var y = new Boolean(false); // (x == y) è true perché x e y hanno lo stesso valore
Quando si utilizza l'operatore ===
Quando si utilizza l'operatore ===
L'operatore deve essere uguale sia per tipo che per valore.
Esempio
var x = false; var y = new Boolean(false); // (x === y) è false perché x e y sono di tipo diverso
O persino peggio. Gli oggetti non possono essere confrontati:
Esempio
var x = new Boolean(false); var y = new Boolean(false); // (x == y) è false perché gli oggetti non possono essere confrontati
Confrontare due oggetti JavaScript sempre restituirà false.
Manuale completo Booleano
Per una guida completa, visitare il nostro Manuale di riferimento Booleano JavaScript.
Il manuale di riferimento include descrizioni e esempi di tutte le proprietà e metodi booleani.
- Pagina precedente JS casuale
- Pagina successiva Confronto JS