Categories
JavaScript Answers

How to convert binary string to decimal with JavaScript?

Sometimes, we want to convert binary string to decimal with JavaScript.

In this article, we’ll look at how to convert binary string to decimal with JavaScript.

How to convert binary string to decimal with JavaScript?

To convert binary string to decimal with JavaScript, we can use the parseInt function.

For instance, we write

const binary = "1101000";
const digit = parseInt(binary, 2);
console.log(digit);

to call parseInt with the binary binary string and 2 to return the binary string parsed into a decimal number.

Conclusion

To convert binary string to decimal with JavaScript, we can use the parseInt function.

Categories
JavaScript Answers

How to sort a ES6 map object with JavaScript?

Sometimes, we want to sort a ES6 map object with JavaScript.

In this article, we’ll look at how to sort a ES6 map object with JavaScript.

How to sort a ES6 map object with JavaScript?

To sort a ES6 map object with JavaScript, we convert the map to an array, sort that, and then convert the sorted array back into a map.

For instance, we write

const sortedMap = new Map(
  [...map].sort((a, b) => {
    // ...
  })
);

to convert the map to an array with [...map].

Then we call sort with a callback to sort the array.

And then we convert the sorted array back to a map using the Map constructor.

Conclusion

To sort a ES6 map object with JavaScript, we convert the map to an array, sort that, and then convert the sorted array back into a map.

Categories
JavaScript Answers

How to properly export an ES6 class in Node.js?

Sometimes, we want to properly export an ES6 class in Node.js.

In this article, we’ll look at how to properly export an ES6 class in Node.js.

How to properly export an ES6 class in Node.js?

To properly export an ES6 class in Node.js, we can set the class as the value of module.exports in a module.

For instance, we write

module.exports = class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  display() {
    console.log(this.firstName, this.lastName);
  }
};

to export the Person class in person.js.

Then we write

const Person = require("./person.js");

const someone = new Person("First name", "Last name");
someone.display();

to import the class with

const Person = require("./person.js");

And then we create a Person object and call the display method.

Conclusion

To properly export an ES6 class in Node.js, we can set the class as the value of module.exports in a module.

Categories
React Answers

How to fix window is not defined in Next.js React app?

Sometimes, we want to fix window is not defined in Next.js React app.

In this article, we’ll look at how to fix window is not defined in Next.js React app.

How to fix window is not defined in Next.js React app?

To fix window is not defined in Next.js React app, we check if window is undefined.

For instance, we write

if (typeof window !== "undefined") {
  // ...
}

to check if window isn’t undefined.

If it’s not, then we can run client side code safely by putting the code in the if block.

Conclusion

To fix window is not defined in Next.js React app, we check if window is undefined.

Categories
JavaScript Answers

How to convert special characters to HTML in JavaScript

Sometimes, we want to convert special characters to HTML in JavaScript.

In this article, we’ll look at how to convert special characters to HTML in JavaScript.

How to convert special characters to HTML in JavaScript

To convert special characters to HTML in JavaScript, we use the String.fromCharCode method.

For instance, we write

const decodeHtmlCharCodes = (str) =>
  str.replace(/(&#(\d+);)/g, (match, capture, charCode) =>
    String.fromCharCode(charCode)
  );

to define the decodeHtmlCharCodes function.

In it, we call str.replace with a regex to match all special characters with the regex.

The callback return the character given the charCode, which we get from the regex matches.

Conclusion

To convert special characters to HTML in JavaScript, we use the String.fromCharCode method.