Categories
JavaScript Answers

How to change an element’s text without changing its child elements with JavaScript?

Sometimes, we want to change an element’s text without changing its child elements with JavaScript.

In this article, we’ll look at how to change an element’s text without changing its child elements with JavaScript.

How to change an element’s text without changing its child elements with JavaScript?

To change an element’s text without changing its child elements with JavaScript, we set the nodeValue property of the text node.

For instance, we write

const div = document.getElementById("foo");
div.firstChild.nodeValue = "New text";

to select the element with getElementById.

Then we get its first child node with firstChild.

And then we set its content by setting the nodeValue to a new value.

Conclusion

To change an element’s text without changing its child elements with JavaScript, we set the nodeValue property of the text node.

Categories
JavaScript Answers

How to check whether something is iterable in JavaScript?

Sometimes, we want to check whether something is iterable in JavaScript.

In this article, we’ll look at how to check whether something is iterable in JavaScript.

How to check whether something is iterable in JavaScript?

To check whether something is iterable in JavaScript, we can check if the Symbol.iterator property of the object if a function.

For instance, we write

const isIterable = (obj) => {
  if (obj === null || obj === undefined) {
    return false;
  }
  return typeof obj[Symbol.iterator] === "function";
};

to check if obj is null or undefined with

obj === null || obj === undefined

If it is, then we return false.

Otherwise, we check if the obj‘s Symbol.iterator property is a function with

typeof obj[Symbol.iterator] === "function"

If that’s true, then obj is an iterable object.

Conclusion

To check whether something is iterable in JavaScript, we can check if the Symbol.iterator property of the object if a function.

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.