Categories
JavaScript Answers

How to join or combine two arrays to concatenate into one array with JavaScript?

Sometimes, we want to join or combine two arrays to concatenate into one array with JavaScript.

In this article, we’ll look at how to join or combine two arrays to concatenate into one array with JavaScript.

How to join or combine two arrays to concatenate into one array with JavaScript?

To join or combine two arrays to concatenate into one array with JavaScript, we can use the spread operator.

For instance, we write

const a = ["a", "b", "c"];
const b = ["d", "e", "f"];

const c = [...a, ...b];

to combine the entries in a and b into array c by spreading the values in them into c with ....

Therefore, c is

["a", "b", "c", "d", "e", "f"]

Conclusion

To join or combine two arrays to concatenate into one array with JavaScript, we can use the spread operator.

Categories
JavaScript Answers

How to substitute values in ES6 template literals at runtime with JavaScript?

Sometimes, we want to substitute values in ES6 template literals at runtime with JavaScript.

In this article, we’ll look at how to substitute values in ES6 template literals at runtime with JavaScript.

How to substitute values in ES6 template literals at runtime with JavaScript?

To substitute values in ES6 template literals at runtime with JavaScript, we can create a function to interpolate values into a template literal and return it.

For instance, we write

const tl = ($) => `This ${$.val}`;
tl({ val: "code" });

to create the tl function that returns the This ${$.val}.

Then we call it with an object with the val property to return a string that has the val property value in the returned string.

Conclusion

To substitute values in ES6 template literals at runtime with JavaScript, we can create a function to interpolate values into a template literal and return it.

Categories
JavaScript Answers

How to remove a property in an object immutably with JavaScript?

Sometimes, we want to remove a property in an object immutably with JavaScript.

In this article, we’ll look at how to remove a property in an object immutably with JavaScript.

How to remove a property in an object immutably with JavaScript?

To remove a property in an object immutably with JavaScript, we can use destructuring.

For instance, we write

const state = {
  c: {
    x: "42",
    y: "43",
  },
};

const {
  c: { y, ...c },
} = state;

console.log({ ...state, c });

to get various properties from the state object with

const {
  c: { y, ...c },
} = state;

And then we spread the properties of state into a new object and put the value of c as a property of the new object with

{ ...state, c }

Conclusion

To remove a property in an object immutably with JavaScript, we can use destructuring.

Categories
JavaScript Answers

How to use JavaScript Object.defineProperty method?

Sometimes, we want to use JavaScript Object.defineProperty method.

In this article, we’ll look at how to use JavaScript Object.defineProperty method.

How to use JavaScript Object.defineProperty method?

To use JavaScript Object.defineProperty method, we can call it with the object we want to add a property to, the property name, and an object with properties to configure the property.

For instance, we write

const employee = {
  firstName: "jane",
  lastName: "smith",
};

Object.defineProperty(employee, "toString", {
  value() {
    return `${this.firstName} ${this.lastName}`;
  },
  writable: true,
  enumerable: true,
  configurable: true,
});

console.log(employee.toString());

to call Object.defineProperty with employee to add the toString method to it.

We define its value as a getter that returns the firstName and lastName properties combined.

And then we set writable to true. to let us reassign it.

enumerable is true means we can return its value with Object.keys, Object.values, etc.

And configurable set to true means we can change the object’s configurability, writability and enumerability.

Conclusion

To use JavaScript Object.defineProperty method, we can call it with the object we want to add a property to, the property name, and an object with properties to configure the property.

Categories
JavaScript Answers

How to replace text inside a div element with JavaScript?

Sometimes, we want to replace text inside a div element with JavaScript.

In this article, we’ll look at how to replace text inside a div element with JavaScript.

How to replace text inside a div element with JavaScript?

To replace text inside a div element with JavaScript, we set the textContent property of the div element.

For instance, we write

const div = document.querySelector("div");
div.textContent = "New text";

to select a div with querySelector.

Then we set its textContent property to 'New text' to render ‘New text’ as the new content of the div.

Conclusion

To replace text inside a div element with JavaScript, we set the textContent property of the div element.