Categories
JavaScript Answers

How to split string in JavaScript by line breaks?

Sometimes, we want to split string in JavaScript by line breaks.

In this article, we’ll look at how to split string in JavaScript by line breaks.

How to split string in JavaScript by line breaks?

To split string in JavaScript by line breaks, we call the string split method with a regex.

For instance, we write

const array = str.split(/\r?\n/);

to call str.split with a regex that matches \r\n or \n to split str into an array with either as separators.

Conclusion

To split string in JavaScript by line breaks, we call the string split method with a regex.

Categories
JavaScript Answers

How to iterate over set elements with JavaScript?

Sometimes, we want to iterate over set elements with JavaScript.

In this article, we’ll look at how to iterate over set elements with JavaScript.

How to iterate over set elements with JavaScript?

To iterate over set elements with JavaScript, we can use the for-of loop.

For instance, we write

const a = new Set();
a.add("Hello");

for (const key of a) {
  console.log(key);
}

to create a set with the Set constructor.

We add an entry into it with add.

Then we use a for-of loop to loop through the entries of a.

We get the entry being looped through from key.

Conclusion

To iterate over set elements with JavaScript, we can use the for-of loop.

Categories
JavaScript Answers

How to get the day of the month in JavaScript?

Sometimes, we want to get the day of the month in JavaScript.

In this article, we’ll look at how to get the day of the month in JavaScript.

How to get the day of the month in JavaScript?

To get the day of the month in JavaScript, we use the getDate method.

For instance, we write

const date = new Date().getDate();

to create a new Date object with the current date and time.

Then we use getDate to get the day of the month.

Conclusion

To get the day of the month in JavaScript, we use the getDate method.

Categories
JavaScript Answers

How to fix preventDefault not working inside passive event listener with JavaScript?

Sometimes, we want to fix preventDefault not working inside passive event listener with JavaScript.

In this article, we’ll look at how to fix preventDefault not working inside passive event listener with JavaScript.

How to fix preventDefault not working inside passive event listener with JavaScript?

To fix preventDefault not working inside passive event listener with JavaScript, we set passive to false.

For instance, we write

document.addEventListener(
  "wheel",
  (e) => {
    e.preventDefault();
    doStuff(e);
  },
  { passive: false }
);

to call addEventListener to add an event listener for the wheel event.

We set passive to false in the object in the 3rd argument so the e.preventDefault will work in the event listener.

Conclusion

To fix preventDefault not working inside passive event listener with JavaScript, we set passive to false.

Categories
JavaScript Answers

How to go back 1 folder level with __dirname with JavaScript?

Sometimes, we want to go back 1 folder level with __dirname with JavaScript.

In this article, we’ll look at how to go back 1 folder level with __dirname with JavaScript.

How to go back 1 folder level with __dirname with JavaScript?

To go back 1 folder level with __dirname with JavaScript, we can use the '../ string.

For instance, we write

const path = require("path");
const p = path.join(__dirname, "../");

to call path.join with __dirname and "../" to return a string with the path one level up from __dirname.

Conclusion

To go back 1 folder level with __dirname with JavaScript, we can use the '../ string.