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.