Categories
JavaScript Answers

How to check if a user has webcam or not using JavaScript?

Spread the love

Sometimes, we want to check if a user has webcam or not using JavaScript.

In this article, we’ll look at how to check if a user has webcam or not using JavaScript.

How to check if a user has webcam or not using JavaScript?

To check if a user has webcam or not using JavaScript, we can use the navigation.getUserMedia method.

For instance, we write:

navigator.getUserMedia({
  video: true
}, () => {
  console.log('has webcam')
}, () => {
  console.log('no webcam')
});

to call it with { video: true } and callbacks that’s run if a webcam is present and if the webcam isn’t present respectively.

Therefore, if the user’s device has a webcam, then 'has webcam' is logged.

Otherwise, 'no webcam' is logged.

Conclusion

To check if a user has webcam or not using JavaScript, we can use the navigation.getUserMedia method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *