Categories
JavaScript Answers

How to Reorder Arrays with JavaScript?

Sometimes, we want to reorder arrays with JavaScript.

In this article, we’ll look at how to reorder arrays with JavaScript.

Reorder Arrays with JavaScript

We can reorder arrays with JavaScript with destructuring.

For instance, we can write:

const swapPositions = (array, a, b) => {
  [array[a], array[b]] = [array[b], array[a]]
}

const array = [1, 2, 3, 4, 5];
swapPositions(array, 0, 1);
console.log(array)

We create the swapPositions array with the array to reorder, and the a and b parameters which are the indexes of array items to swap.

In the function body, we swap the position of the elements with indexes a and b by putting them both in an array and then destructuring them.

This will swap the 2 array items in place.

Therefore, when we call swapPositions with array , 0, and 1, we get:

[2, 1, 3, 4, 5]

as a result.

Conclusion

We can reorder arrays with JavaScript with destructuring.

Categories
JavaScript Answers

How to Detect When the Mouse Leaves the Window with JavaScript?

Sometimes, we want to detect when the mouse leaves the window with JavaScript.

In this article, we’ll look at how to detect when the mouse leaves the window with JavaScript.

Detect When the Mouse Leaves the Window with JavaScript

To detect when the mouse leaves the window with JavaScript, we can add the mouseleave event to document .

For instance, we can write:

document.addEventListener("mouseleave", (event) => {  
  if (event.clientY <= 0 || event.clientX <= 0 || (event.clientX >= window.innerWidth || event.clientY >= window.innerHeight)) {  
    console.log("I'm out");  
  }  
});

We add a mouseleave event listener with the addEventListener method.

Then in the event listener, we check if one of the following conditions are met:

  • event.clientY is less than or equal to 0
  • event.clientX is less than or equal to 0
  • event.clientX is bigger than or equal to window.innerWidth
  • event.clientY is bigger than or equal to window.innerHeight

If any of the conditions are true , then we know the mouse is out of the browser tab.

And therefore, “I'm out" is logged.

Conclusion

To detect when the mouse leaves the window with JavaScript, we can add the mouseleave event to document .

Categories
JavaScript Answers

How to Convert HH:MM:SS Time String to Seconds Only in JavaScript?

Sometimes, we want to convert a HH:MM:SS time string to seconds only in JavaScript.

In this article, we’ll look at how to convert a HH:MM:SS time string to seconds only in JavaScript.

Convert HH:MM:SS Time String to Seconds Only in JavaScript

We can convert a HH:MM:SS time string to seconds only in JavaScript by converting the hours and minutes to seconds.

And then add the converted values to the seconds’ portion of the time.

For instance, we can write:

const hms = '02:04:33';
const [hours, minutes, seconds] = hms.split(':');
const totalSeconds = (+hours) * 60 * 60 + (+minutes) * 60 + (+seconds);
console.log(totalSeconds);

We have the hms string in HH:MM:SS format.

Then we split by the colons with the split method.

Next, we get the hours and convert it to a number with the + operator and multiply by 60* 60 to convert that to seconds.

Then we add that to the minutes converted to a number and multiplied by 60 to convert the minutes to seconds.

And then we add both to the seconds to get the totalSeconds .

As a result, totalSeconds is 7473.

Conclusion

We can convert a HH:MM:SS time string to seconds only in JavaScript by converting the hours and minutes to seconds.

Categories
JavaScript Answers

How to Check if the DOM is Ready without Any JavaScript Framework or Library?

Sometimes, we want to check if the DOM is ready without any JavaScript framework or library.

In this article, we’ll look at how to check if the DOM is ready without any JavaScript framework or library.

Check if the DOM is Ready without Any JavaScript Framework or Library

To check if the DOM is ready without any JavaScript framework or library, we can listen to the DOMContentLoaded or the load event.

Inside the event handlers for each event, we can check the value of the document.readyState property to determine if the DOM is ready or not.

For instance, we can write:

window.addEventListener("DOMContentLoaded", () => {
  if (document.readyState === "complete") {
    console.log('loaded')
  } else if (document.readyState === "interactive") {
    // DOM ready! Images, frames, and other subresources are still downloading.
  }
});

window.addEventListener("load", () => {
  if (document.readyState === "complete") {
    console.log('loaded')
  } else if (document.readyState === "interactive") {
    // DOM ready! Images, frames, and other subresources are still downloading.
  }
});

We listen for the DOMContentLoaded and load events by calling window.addEventListener .

Then in each event handler, we check the document.readyState value.

If the value is 'complete' , then we know the DOM is fully loaded.

If it’s 'interactive' , then the DOM is ready, but images, frames, and other resources are still loading.

Conclusion

To check if the DOM is ready without any JavaScript framework or library, we can listen to the DOMContentLoaded or the load event.

Inside the event handlers for each event, we can check the value of the document.readyState property to determine if the DOM is ready or not.

Categories
JavaScript Answers

How to Get an Element’s Padding Value Using JavaScript?

Sometimes, we want to get an element’s padding value using JavaScript.

In this article, we’ll look at how to get an element’s padding value using JavaScript.

Get an Element’s Padding Value Using JavaScript

To get an element’s padding value using JavaScript, we can use the getComputedStyle and getPropertyValue methods.

For instance, if we have the following HTML:

<div style='padding: 20px'>  
  hello world  
</div>

Then we can get the padding-left value of the div by writing:

const div = document.querySelector('div')  
const paddingLeft = window.getComputedStyle(div, null).getPropertyValue('padding-left')  
console.log(paddingLeft)

We call document.querySelector to get the div.

Then we call window.getComputedStyle with the div to get the computed CSS styles of the div.

Then we call getPropertyValue with 'padding-left' to get the padding-left CSS property value.

Therefore, paddingLeft is '20px' according to the console log.

Conclusion

To get an element’s padding value using JavaScript, we can use the getComputedStyle and getPropertyValue methods.