Categories
JavaScript Answers

How to access camera from a browser with JavaScript?

To access camera from a browser with JavaScript, we use the navigator.mediaDevices.getUserMedia method.

For instance, we write

<video src=""></video>

to add a video element.

Then we write

const front = false;
const video = document.querySelector("video");
const constraints = {
  video: {
    facingMode: front ? "user" : "environment",
    width: 640,
    height: 480,
  },
};

const mediaStream = await navigator.mediaDevices.getUserMedia(constraints);
video.srcObject = mediaStream;
video.onloadedmetadata = (e) => {
  video.play();
};

to select the video element with querySelector.

We get permission for the camera with navigator.mediaDevices.getUserMedia.

And then we set the mediaStream as the src attribute value of the video element if permission is granted.

Finally, we call play to play the captured video.

Categories
JavaScript Answers

How to create a method for a custom object in JavaScript?

Sometimes, we want to create a method for a custom object in JavaScript.

In this article, we’ll look at how to create a method for a custom object in JavaScript.

How to create a method for a custom object in JavaScript?

To create a method for a custom object in JavaScript, we set a property to a function.

For instance, we write

const newObj = {
  met1() {
    alert("hello");
  },
};

newObj.met1();

to put the met1 method into the newObj object.

Then we call met1 with newObj.met1();.

Conclusion

To create a method for a custom object in JavaScript, we set a property to a function.

Categories
JavaScript Answers

How to use JavaScript to sort contents of select element?

Sometimes, we want to use JavaScript to sort contents of select element.

In this article, we’ll look at how to use JavaScript to sort contents of select element.

How to use JavaScript to sort contents of select element?

To use JavaScript to sort contents of select element, we use the sort method.

For instance, we write

const optionNodes = Array.from(selectNode.children);
const comparator = new Intl.Collator(lang.slice(0, 2)).compare;

const sorted = optionNodes.sort((a, b) =>
  comparator(a.textContent, b.textContent)
);

to call Array.from to convert the node list with select element’s child elements into an array.

Then we get the compare function from the Intl.Collator constructor.

And then we call sort with a callback that calls comparator to return an array sorted by the child element’s text content.

Conclusion

To use JavaScript to sort contents of select element, we use the sort method.

Categories
JavaScript Answers

How to get the current date in DD-Mon-YYYY format in JavaScript?

Sometimes, we want to get the current date in DD-Mon-YYY format in JavaScript.

In this article, we’ll look at how to get the current date in DD-Mon-YYY format in JavaScript.

How to get the current date in DD-Mon-YYY format in JavaScript?

To get the current date in DD-Mon-YYY format in JavaScript, we call the format method.

For instance, we write

const dateStr = moment().format("DD-MMM-YYYY");

to call moment to return a moment object with the current date time.

And then we call format to return a string in the DD-Mon-YYYY format.

Conclusion

To get the current date in DD-Mon-YYY format in JavaScript, we call the format method.

Categories
JavaScript Answers

How to find last index of element inside array by certain condition with JavaScript?

Sometimes, we want to find last index of element inside array by certain condition with JavaScript.

In this article, we’ll look at how to find last index of element inside array by certain condition with JavaScript.

How to find last index of element inside array by certain condition with JavaScript?

To find last index of element inside array by certain condition with JavaScript, we use the map and lastIndexOf methods.

For instance, we write

const lastIndex = elements.map((e) => e.a).lastIndexOf("something");

to call map and lastIndex to return an array with the a property value of each item in elements.

And then we call lastIndexOf to look for the last index of 'something' in the returned array.

Conclusion

To find last index of element inside array by certain condition with JavaScript, we use the map and lastIndexOf methods.