Categories
JavaScript Answers

How to create a function returning an object with JavaScript?

Sometimes, we want to create a function returning an object with JavaScript.

In this article, we’ll look at how to create a function returning an object with JavaScript.

How to create a function returning an object with JavaScript?

To create a function returning an object with JavaScript, we can make a function that returns an object literal.

For instance, we write

const makeGamePlayer = (name, totalScore, gamesPlayed) => {
  return {
    name,
    totalscore,
    gamesPlayed,
  };
};

to define the makeGamePlayer function that returns an object with the name, totalScore, and gamesPlayed properties.

Conclusion

To create a function returning an object with JavaScript, we can make a function that returns an object literal.

Categories
JavaScript Answers

How to add new li to ul on click with JavaScript?

Sometimes, we want to add new li to ul on click with JavaScript.

In this article, we’ll look at how to add new li to ul on click with JavaScript.

How to add new li to ul on click with JavaScript?

To add new li to ul on click with JavaScript, we can call the appendChild method.

For instance, we write

const ul = document.getElementById("list");
const li = document.createElement("li");
li.appendChild(document.createTextNode("Element 4"));
ul.appendChild(li);

in our click handler function.

We call getElementById to select the ul element.

Then we call createElement to create an li element.

Next, we call createTextNode to create a text node.

Then we call ul.appendChild to append the li as the last child of the ul element.

Conclusion

To add new li to ul on click with JavaScript, we can call the appendChild method.

Categories
JavaScript Answers

How to tell if a video element is currently playing with JavaScript?

Sometimes, we want to tell if a video element is currently playing with JavaScript.

In this article, we’ll look at how to tell if a video element is currently playing with JavaScript.

How to tell if a video element is current

To tell if a video element is currently playing with JavaScript, we check a few properties in the video element.

For instance, we write

const isVideoPlaying = (video) =>
  !!(
    video.currentTime > 0 &&
    !video.paused &&
    !video.ended &&
    video.readyState > 2
  );

to define the isVideoPlaying function that takes a video element object.

In it, we check if the currentTime is bigger than 0 seconds.

We then check it’s not paused with paused and negating that.

We check it’s not ended with ended and negating that.

And then we check that its readyState is bigger than 2, which means the video is loaded.

If they’re all truthy, then the video is playing.

Conclusion

To tell if a video element is currently playing with JavaScript, we check a few properties in the video element.

Categories
JavaScript Answers

How to remove array element on condition with JavaScript?

Sometimes, we want to remove array element on condition with JavaScript.

In this article, we’ll look at how to remove array element on condition with JavaScript.

How to remove array element on condition with JavaScript?

To remove array element on condition with JavaScript, we can use the array filter method.

For instance, we write

let ar = [1, 2, 3, 4];
ar = ar.filter((item) => !(item > 3));

to call ar.filter with a callback to filter out all the items that are bigger than 3.

Then we assign the returned array back to ar.

Therefore, the new value of ar is [1, 2, 3].

Conclusion

To remove array element on condition with JavaScript, we can use the array filter method.

Categories
JavaScript Answers

How to detect click outside element with JavaScript?

Sometimes, we want to detect click outside element with JavaScript.

In this article, we’ll look at how to detect click outside element with JavaScript.

How to detect click outside element with JavaScript?

To detect click outside element with JavaScript, we can use the element’s contains method.

For instance, we write

const specifiedElement = document.getElementById("a");

document.addEventListener("click", (event) => {
  const isClickInside = specifiedElement.contains(event.target);

  if (!isClickInside) {
    // ...
  }
});

to select the element we want to check with getElemebntById.

Then we add a click listener for the whole page with document.addEventListener.

In the callback, we call specifiedElement.contains with event.target to check if we clicked inside the a element.

If it’s false, then we clicked outside of it.

Conclusion

To detect click outside element with JavaScript, we can use the element’s contains method.