Javascript, What does the ^ (caret) operator do?

Torres picture Torres · Sep 1, 2010 · Viewed 53.5k times · Source

I have some javascript code:

<script type="text/javascript">
$(document).ready(function(){
  $('#calcular').click(function() {
    var altura2 = ((($('#ddl_altura').attr("value"))/100)^2);
    var peso = $('#ddl_peso').attr("value");
    var resultado = Math.round(parseFloat(peso / altura2)*100)/100;
    if (resultado > 0) {
      $('#resultado').html(resultado);
      $('#imc').show();
    };
  });
});
</script>

What does the ^ (caret) operator mean in Javascript?

Answer

Gumbo picture Gumbo · Sep 1, 2010

The ^ operator is the bitwise XOR operator. To square a value, use Math.pow:

var altura2 = Math.pow($('#ddl_altura').attr("value")/100, 2);