Convierta el resultado booleano en número/entero en JavaScript

    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:

  1. Usar operador ternario o condicional
  2. Usando el operador unario +.
  3. Usando el operador bit a bit Y (&) u bit a bit O ( | ).
  4. Usando la función Number(). Convierte el tipo de datos en número.
    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:
    Output unary
    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:
    output bitwise
    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:
    Número de salida()

Publicación traducida automáticamente

Artículo escrito por vishodushaozae y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *