Categories
JavaScript Answers

How to set image source with base64 with JavaScript?

Sometimes, we want to set image source with base64 with JavaScript.

In this article, we’ll look at how to set image source with base64 with JavaScript.

How to set image source with base64 with JavaScript?

To set image source with base64 with JavaScript, we can set the src property to a base64 URL string.

For instance, we write

document.getElementById("img").src =
  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";

to select the img element with getElementById.

Then we set its src property to the base64 image URL string to set the src attribute of the img element to that value.

Conclusion

To set image source with base64 with JavaScript, we can set the src property to a base64 URL string.

Categories
JavaScript Answers

How to fix Node.js Express.js app only working on port 3000 with JavaScript?

Sometimes, we want to fix Node.js Express.js app only working on port 3000 with JavaScript.

In this article, we’ll look at how to fix Node.js Express.js app only working on port 3000 with JavaScript.

How to fix Node.js Express.js app only working on port 3000 with JavaScript?

To fix Node.js Express.js app only working on port 3000 with JavaScript, we can set the port with the process.env.PORT property.

For instance, we write

app.set("port", process.env.PORT || 3000);

to set the port of the Express app with app.set.

We try to get the port number from the PORT environment variable, which is process.env.PORT.

Otherwise, we use port 3000 as the fallback.

Then we start our Node Express app by running

PORT=8080 node app.js

to set the PORT environment variable to 8080 and run app.js which has the app.set code.

Conclusion

To fix Node.js Express.js app only working on port 3000 with JavaScript, we can set the port with the process.env.PORT property.

Categories
JavaScript Answers

How to force JavaScript to do math instead of putting two strings together?

Sometimes, we want to force JavaScript to do math instead of putting two strings together.

In this article, we’ll look at how to force JavaScript to do math instead of putting two strings together.

How to force JavaScript to do math instead of putting two strings together?

To force JavaScript to do math instead of putting two strings together, we should make sure we’re using arithmetic operators with numbers.

For instance, we write

const x = +"11.5" + +"3.5";

to convert '11.5' and '3.5' to numbers with the unary + operator.

Then we add the returned numbers together and assign the sum to x.

Conclusion

To force JavaScript to do math instead of putting two strings together, we should make sure we’re using arithmetic operators with numbers.

Categories
JavaScript Answers

How to remove all classes that begin with a certain string with JavaScript?

Sometimes, we want to remove all classes that begin with a certain string with JavaScript.

In this article, we’ll look at how to remove all classes that begin with a certain string with JavaScript.

How to remove all classes that begin with a certain string with JavaScript?

To remove all classes that begin with a certain string with JavaScript, we use the string startsWith method.

For instance, we write

const prefix = "prefix";
const classes = el.className.split(" ").filter((c) => {
  return !c.startsWith(prefix);
});
el.className = classes.join(" ").trim();

to call el.className.split to split the className string by the spaces into an array of substrings.

Then we call filter with a callback that filters out all entries that starts with prefix.

Next, we call join to join all the class name strings in the classes array back into a string separated by spaces.

Then we call trim to remove start and ending whitespaces.

Conclusions

To remove all classes that begin with a certain string with JavaScript, we use the string startsWith method.

Categories
JavaScript Answers

How to check if the array of objects have duplicate property values with JavaScript?

Sometimes, we want to check if the array of objects have duplicate property values with JavaScript.

In this article, we’ll look at how to check if the array of objects have duplicate property values with JavaScript.

How to check if the array of objects have duplicate property values with JavaScript?

To check if the array of objects have duplicate property values with JavaScript, we create a set from the property of each array entry that we want to check for duplicates for.

For instance, we write

const values = [
  { name: "someName1" },
  { name: "someName2" },
  { name: "someName3" },
  { name: "someName1" },
];

const uniqueValues = new Set(values.map((v) => v.name));

if (uniqueValues.size < values.length) {
  console.log("duplicates found");
}

to get the name property values of each values array entry with

values.map((v) => v.name)

Then we convert the array to a set with the Set constructor.

If the set’s size is less than valueslength, then we know there’re duplicates values in values.

Conclusion

To check if the array of objects have duplicate property values with JavaScript, we create a set from the property of each array entry that we want to check for duplicates for.