Categories
JavaScript Answers

How to create object using variables for property name with JavaScript?

Sometimes, we want to create object using variables for property name with JavaScript.

In this article, we’ll look at how to create object using variables for property name with JavaScript.

How to create object using variables for property name with JavaScript?

To create object using variables for property name with JavaScript, we can use the computed property name syntax.

For instance, we write

const create = (propertyName) => {
  const myObject = { [propertyName]: "value" };
  return myObject;
};

to define the create function that takes the propertyName string parameter.

In it, we use the return value of propertyName as the property name.

And we set its value to 'value'.

Conclusion

To create object using variables for property name with JavaScript, we can use the computed property name syntax.

Categories
JavaScript Answers

How to paste an image from clipboard using JavaScript?

Sometimes, we want to paste an image from clipboard using JavaScript.

In this article, we’ll look at how to paste an image from clipboard using JavaScript.

How to paste an image from clipboard using JavaScript?

To paste an image from clipboard using JavaScript, we can listen to the document‘s paste event.

For instance, we write

document.onpaste = (event) => {
  const items = (event.clipboardData ?? event.originalEvent.clipboardData)
    .items;
  for (const item of items) {
    if (item.kind === "file") {
      const blob = item.getAsFile();
      const reader = new FileReader();
      reader.onload = (event) => {
        console.log(event.target.result);
      };
      reader.readAsDataURL(blob);
    }
  }
};

to set document.onpaste to a function that runs when we paste something onto the page.

In it, we get the pasted items from event.clipboardData or event.originalEvent.clipboardData.

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

In it, we check if item.kind is a 'file'.

If it is, we call item.getAsFile to get the pasted item as a blob.

And we use the FileReader instance’s onload method get the read file.

The read file’s content is in event.target.result.

It’s a base64 string since we read it with readAsDataURL.

Conclusion

To paste an image from clipboard using JavaScript, we can listen to the document‘s paste event.

Categories
JavaScript Answers

How to prevent SQL injection in Node.js?

Sometimes, we want to prevent SQL injection in Node.js.

In this article, we’ll look at how to prevent SQL injection in Node.js.

How to prevent SQL injection in Node.js?

To prevent SQL injection in Node.js, we can use parameterized queries.

For instance, we write

const userId = 5;
const query = connection.query(
  "SELECT * FROM users WHERE id = ?",
  [userId],
  (err, results) => {
    // ...
  }
);

to call the Node mysql package’s connection.query method with a string that has the ? placeholder for the id value.

The 2nd argument is an array with the values to fill the placeholders with.

And the last argument is a callback with the results returning the query results.

userId will be escaped before the query is made so eliminate the risk of SQL injection.

Conclusion

To prevent SQL injection in Node.js, we can use parameterized queries.

Categories
JavaScript Answers

How to find the day, month and year with moment.js and JavaScript?

Sometimes, we want to find the day, month and year with moment.js and JavaScript.

In this article, we’ll look at how to find the day, month and year with moment.js and JavaScript.

How to find the day, month and year with moment.js and JavaScript?

To find the day, month and year with moment.js and JavaScript, we can use the date, month, and year methods.

For instance, we write

const date = moment().date();
const month = moment().month();
const year = moment().year();

to call moment to return a moment object with the current date and time.

Then we call date to return the day of the month.

We call month to return the month. month returns 0 for January and up to 11 for December.

And we call year to return the year.

Conclusion

To find the day, month and year with moment.js and JavaScript, we can use the date, month, and year methods.

Categories
JavaScript Answers

How to log an HTML element as a JavaScript object?

Sometimes, we want to log an HTML element as a JavaScript object.

In this article, we’ll look at how to log an HTML element as a JavaScript object.

How to log an HTML element as a JavaScript object?

To log an HTML element as a JavaScript object, we can use the console.dir method.

For instance, we write

const element = document.documentElement;
console.dir(element);

to get the document element with document.documentElement.

Then we assign that to element and log that with console.dir.

As a result, the properties of element will be displayed in the console.

Conclusion

To log an HTML element as a JavaScript object, we can use the console.dir method.