title: How to switch between front camera and rear camera in javascript?
body: <pre class="lang-js prettyprint-override"><code>let constraints;
  function handleVideo(){
  const constraints = {
    video: {
      facingMode: {
        exact: 'user'
      }
    }
  };
  
  var video;
  navigator.mediaDevices.getUserMedia(constraints).
  then((stream) =&gt; {
    video = document.createElement(&quot;video&quot;)
    video.srcObject = stream
    video.play()
    video.onloadeddata = () =&gt; {
      ctx.height = video.videoHeight
    }
  })
  }
  </code></pre>
  <p>I know by changing <code>exact</code> to <code>environment</code> can switch between front and back camera. But I don't know how to do this onclick.</p>