Categories
JavaScript Answers

How to detect if user is scrolling with JavaScript?

Sometimes, we want to detect if user is scrolling with JavaScript.

In this article, we’ll look at how to detect if user is scrolling with JavaScript.

How to detect if user is scrolling with JavaScript?

To detect if user is scrolling with JavaScript, we can listen to the window’s scroll event.

For instance, we write

let userHasScrolled = false;
window.onscroll = (e) => {
  userHasScrolled = true;
};

to set window.onscroll to a function that sets userHasScrolled to true when we scroll in the window.

Then if userHasScrolled is true, we know the user scrolled.

Conclusion

To detect if user is scrolling with JavaScript, we can listen to the window’s scroll event.

Categories
JavaScript Answers

How to print JavaScript exception stack trace?

Sometimes, we want to print JavaScript exception stack trace.

In this article, we’ll look at how to print JavaScript exception stack trace.

How to print JavaScript exception stack trace?

To print JavaScript exception stack trace, we can use the stack property of an Error instance.

For instance, we write

console.log(new Error().stack);

to create a new Error instance and then get the stack trace from the stack property.

Then we use console.log to log the stack trace.

We put this in the line where we want to print the stack trace to the console.

Conclusion

To print JavaScript exception stack trace, we can use the stack property of an Error instance.

Categories
JavaScript Answers

How to sort or order keys in JavaScript objects?

Sometimes, we want to sort or order keys in JavaScript objects.

In this article, we’ll look at how to sort or order keys in JavaScript objects.

How to sort or order keys in JavaScript objects?

To sort or order keys in JavaScript objects, we can use the Object.keys method and the array sort and reduce methods.

For instance, we write

const notSorted = { b: false, a: true };

const sorted = Object.keys(notSorted)
  .sort()
  .reduce(
    (acc, key) => ({
      ...acc,
      [key]: notSorted[key],
    }),
    {}
  );

console.log(sorted);

to call Object.keys with notSorted to get the keys of the notSorted object in an array.

Then we call sort to sort the keys.

Then we call reduce with the sorted keys array with a callback to get the keys and values and put them in a new object.

The new object with the sort keys is then returned by reduce.

Conclusion

To sort or order keys in JavaScript objects, we can use the Object.keys method and the array sort and reduce methods.

Categories
JavaScript Answers

How to sort an array so that null values always come last with JavaScript?

Sometimes, we want to sort an array so that null values always come last with JavaScript.

In this article, we’ll look at how to sort an array so that null values always come last with JavaScript.

How to sort an array so that null values always come last with JavaScript?

To sort an array so that null values always come last with JavaScript, we can use the array sort method.

For instance, we write

const sorted = arr.sort(
  (a, b) => (a !== null ? a : Infinity) - (b !== null ? b : Infinity)
);

to call arr.sort with a callback that sorts non-null values in ascending order.

We check if a or b is null.

If they are, we return Infinity as its value and use that for sorting.

To sort in descending order, we flip the expressions in the callback by writing

const sorted = arr.sort(
  (a, b) => (b !== null ? b : -Infinity) - (a !== null ? a : -Infinity)
);

Conclusion

To sort an array so that null values always come last with JavaScript, we can use the array sort method.

Categories
JavaScript Answers

How to convert a char array to a string with JavaScript?

Sometimes, we want to convert a char array to a string with JavaScript.

In this article, we’ll look at how to convert a char array to a string with JavaScript.

How to convert a char array to a string with JavaScript?

To convert a char array to a string with JavaScript, we use the array join method.

For instance, we write

const str = s.join("");

to call s.join with an empty string to join the character strings in array s into the str string.

join takes a separator between the strings, so passing in an empty string means there’s no separator between the characters.

Conclusion

To convert a char array to a string with JavaScript, we use the array join method.