- Usar operador ternario o condicional
- Usando el operador unario +.
- Usando el operador bit a bit Y (&) u bit a bit O ( | ).
- Usando la función Number(). Convierte el tipo de datos en número.
Un valor booleano de JavaScript representa uno de dos valores: verdadero o falso .
Sin embargo, si uno quiere convertir una variable que almacena un valor booleano, en un número entero «0» o «1» , puede hacerlo utilizando múltiples enfoques. Analizaremos algunos de ellos en este artículo.
Los métodos más populares son:
- Usando el operador ternario o condicional:
- Sintaxis:
var i = value ? 1 : 0;
- Programa:
<!DOCTYPE html>
<html>
<body>
<center>
<h1 style=
"color:green"
>GeeksforGeeks</h1>
<h4>Click the button to change the boolean
value into number.</h4>
<script>
// Initializing boolvalue as true
var
boolvalue =
true
</script>
<button onclick=
"myFunction()"
>Change</button>
<p>The number value of the variable is :</p>
<p id=
"result"
></p>
<script>
// JavaScript program to illustrate boolean
// conversion using ternary operator
function
myFunction() {
var
i = boolvalue ? 1 : 0;
document.getElementById(
"result"
).innerHTML = i;
}
</script>
</center>
</body>
</html>
- Salida después de hacer clic en el botón:
- Usando el operador unario + :
- Sintaxis:
var i = + boolvalue;
- Programa:
<!DOCTYPE html>
<html>
<body>
<center>
<h1 style=
"color:green"
>GeeksforGeeks</h1>
<p>Click the button to change the boolean value.</p>
<script>
// Initializing boolvalue as true
var
boolvalue =
true
;
</script>
<button onclick=
"myFunction()"
>Change</button>
<p>The value of the variable is now:</p>
<p id=
"result"
></p>
<script>
// JavaScript program to illustrate boolean
// conversion using unary operator
function
myFunction(){
var
i = + boolvalue;
document.getElementById(
"result"
).innerHTML = i;
}
</script>
</body>
</html>
- Salida después de hacer clic en el botón:
- Usando el operador bit a bit Y (&) u bit a bit O ( | ).
- Sintaxis:
var i = boolvalue & 1; // bitwise and var j = boolvalue | 0; // bitwise or
- Programa:
<!DOCTYPE html>
<html>
<body>
<center>
<h1 style=
"color:green"
>GeeksforGeeks</h1>
<p>Click the button to change the boolean value.</p>
<script>
// Initializing boolvalue as true
var
boolvalue =
true
;
// Initializing boolvalue2 as false
var
boolvalue2 =
false
;
</script>
<button onclick=
"myFunction()"
>Change</button>
<p>The value of the variable 1 is now:</p>
<p id=
"result"
></p>
<p>The value of the variable 2 is now:</p>
<p id=
"result2"
></p>
<script>
// JavaScript program to illustrate boolean
// conversion using bitwise operator
function
myFunction(){
var
i = boolvalue & 1;
var
j = boolvalue2 | 0;
document.getElementById(
"result"
).innerHTML = i;
document.getElementById(
"result2"
).innerHTML = j;
}
</script>
</body>
</html>
- Salida después de hacer clic en el botón:
- Usando la función Number() . Convierte el tipo de datos en número.:
- Sintaxis:
var i = Number(boolvalue);
- Programa:
<!DOCTYPE html>
<html>
<body>
<center>
<h1 style=
"color:green"
>GeeksforGeeks</h1>
<p>Click the button to change the boolean value.</p>
<script>
// Initializing boolvalue as true
var
boolvalue =
true
;
</script>
<button onclick=
"myFunction()"
>Change</button>
<p>The value of the variable is now:</p>
<p id=
"result"
></p>
<script>
// JavaScript program to illustrate boolean
// conversion using Number() function
function
myFunction(){
var
i = Number(boolvalue);
document.getElementById(
"result"
).innerHTML = i;
}
</script>
</body>
</html>
Salida después de hacer clic en el botón:
Publicación traducida automáticamente
Artículo escrito por vishodushaozae y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA