Categories
JavaScript Answers

How to append to new line in Node.js?

Sometimes, we want to append to new line in Node.js.

In this article, we’ll look at how to append to new line in Node.js.

How to append to new line in Node.js?

To append to new line in Node.js, we can use the os.EOL constant.

For instance, we write

const os = require("os");

const processInput = (text) => {
  fs.open("H://log.txt", "a", 666, (e, id) => {
    fs.write(id, text + os.EOL, null, "utf8", () => {
      fs.close(id, () => {
        console.log("file is updated");
      });
    });
  });
};

to log the log.txt file with append permission with fs.open.

In the fs.open callback, we call fs.write to add the os.EOL end of line character after the text.

Then we call fs.close in the write callback to close the file.

Conclusion

To append to new line in Node.js, we can use the os.EOL constant.

Categories
JavaScript Answers

How to scroll long page to div with JavaScript?

Sometimes, we want to scroll long page to div with JavaScript.

In this article, we’ll look at how to scroll long page to div with JavaScript.

How to scroll long page to div with JavaScript?

To scroll long page to div with JavaScript, we can select the element to scroll to and call scrollToView on it.

For instance, we write

document.getElementById("youridhere").scrollIntoView();

to select the element with ID youridhere with getElementById.

Then we call scrollIntoView to scroll to the element.

Conclusion

To scroll long page to div with JavaScript, we can select the element to scroll to and call scrollToView on it.

Categories
JavaScript Answers

How to send emails with JavaScript?

Sometimes, we want to send emails with JavaScript.

In this article, we’ll look at how to send emails with JavaScript.

How to send emails with JavaScript?

To send emails with JavaScript, we can create a mailto URL and go to it.

For instance, we write

const sendMail = () => {
  const link =
    "mailto:me@example.com" +
    "?cc=myCCaddress@example.com" +
    "&subject=" +
    encodeURIComponent("This is my subject") +
    "&body=" +
    encodeURIComponent(document.getElementById("myText").value);
  window.location.href = link;
};

to define the sendMail function that creates the link mailto URL.

mailto: has the address to send the email to.

cc has the CC email.

subject has the subject.

body has the email body.

We use encodeURIComponent to encode the values so that they can be in the URL.

Then we set window.location.href to link to open the default mail client with the data from the URL filled as the value in the fields.

Conclusion

To send emails with JavaScript, we can create a mailto URL and go to it.

Categories
JavaScript Answers

How to get all the cookies from the browser with JavaScript?

Sometimes, we want to get all the cookies from the browser with JavaScript.

In this article, we’ll look at how to get all the cookies from the browser with JavaScript.

How to get all the cookies from the browser with JavaScript?

To get all the cookies from the browser with JavaScript, we can get them from the document.cookie string.

For instance, we write

const c = document.cookie.split(";").reduce((ac, cv, i) => {
  const [key, val] = cv.split("=");
  return Object.assign(ac, { [key]: val });
}, {});

console.log(c);

to get the cookie string with document.cookie.

Then we split the string by the semicolons and return an array of cookie strings.

Next, we call reduce with a callback that splits the cv string into the key and val strings with split.

And we return an object that merges the { [key]: val } object into the ac object with Object.assign.

Then c is an object with all the cookies with the cookie keys as property keys and the corresponding values as values.

Conclusion

To get all the cookies from the browser with JavaScript, we can get them from the document.cookie string.

Categories
JavaScript Answers

How to remove commas from string using JavaScript?

Sometimes, we want to remove commas from string using JavaScript.

In this article, we’ll look at how to remove commas from string using JavaScript.

How to remove commas from string using JavaScript?

To remove commas from string using JavaScript, we can use the string replace method.

For instance, we write

const total = parseFloat("100,000.00".replace(/,/g, ""));

to return a string that has the commas removed from "100,000.00" by calling replace on it with the /,/g regex and an empty string.

The g flag searches for all instances of the pattern.

Then we call parseFloat with the returned string to convert the string to a number.

Conclusion

To remove commas from string using JavaScript, we can use the string replace method.