Categories
JavaScript Answers

How to remove characters from a string with JavaScript?

Sometimes, we want to remove characters from a string with JavaScript.

In this article, we’ll look at how to remove characters from a string with JavaScript.

How to remove characters from a string with JavaScript?

To remove characters from a string with JavaScript, we use the string replace method.

For instance, we write

const str = "foo bar baz";
const newStr = str.replace(/ba/gi, "");

to call str.replace with a regex that matches the pattern we want to remove.

And we replace all matches with empty strings.

The g flag makes replace replace all matches.

i matches it find matches in a case-insensitive manner.

Conclusion

To remove characters from a string with JavaScript, we use the string replace method.

Categories
React Answers

How to delay rendering of React components?

Sometimes, we want to delay rendering of React components.

In this article, we’ll look at how to delay rendering of React components.

How to delay rendering of React components?

To delay rendering of React components, we use the setTimeout function.

For instance, we write

import React, { useState, useEffect } from "react";

const Delayed = ({ children, waitBeforeShow = 500 }) => {
  const [isShown, setIsShown] = useState(false);

  useEffect(() => {
    const timer = setTimeout(() => {
      setIsShown(true);
    }, waitBeforeShow);

    return () => clearTimeout(timer);
  }, [waitBeforeShow]);

  return isShown ? children : null;
};

export default Delayed;

to create the Delayed component.

It renders the children when isShown is true.

isShown is set to true in the setTimeout callback with setIsShown(true); after waitBeforeShow milliseconds, which is in the useEffect callback.

When waitBeforeShow is changed, the useEffect callback is called.

clearTimeout is called right before waitBeforeShow is changed.

Then we use it by writing

export const LoadingScreen = () => {
  return (
    <Delayed>
      <div />
    </Delayed>
  );
};

Conclusion

To delay rendering of React components, we use the setTimeout function.

Categories
JavaScript Answers

How to move item in array to last position with JavaScript?

Sometimes, we want to move item in array to last position with JavaScript.

In this article, we’ll look at how to move item in array to last position with JavaScript.

How to move item in array to last position with JavaScript?

To move item in array to last position with JavaScript, we use the shift and push methods.

For instance, we write

const a = [5, 1, 2, 3, 4];
a.push(a.shift());

to call a.shift to return the first item from array a after removing it.

Then we call a.push with the return item to put it as the last item in a.

Conclusion

To move item in array to last position with JavaScript, we use the shift and push methods.

Categories
JavaScript Answers

How to get value of span text with JavaScript?

Sometimes, we want to get value of span text with JavaScript.

In this article, we’ll look at how to get value of span text with JavaScript.

How to get value of span text with JavaScript?

To get value of span text with JavaScript, we use the innerText property.

For instance, we write

<span id="span_Id">I am the Text </span>

to add a span.

Then we write

const spanText = document.getElementById("span_Id").innerText;

console.log(spanText);

to select the span with getElementById.

Then we get the text content with the innerText property.

Conclusion

To get value of span text with JavaScript, we use the innerText property.

Categories
JavaScript Answers

How to add optional first argument in function with JavaScript?

Sometimes, we want to add optional first argument in function with JavaScript.

In this article, we’ll look at how to add optional first argument in function with JavaScript.

How to add optional first argument in function with JavaScript?

To add optional first argument in function with JavaScript, we can make our function accept an object.

For instance, we write

const myFunction = (hash) => {
  const { options, content } = hash;
  //...
};

myFunction({ options, content });

to define the myFunction function that takes the hash object.

In it, we get the options and content properties from the hash.

And we call myFunction with an object with those 2 properties.

We can omit any property in the object we call myFunction with.

Conclusion

To add optional first argument in function with JavaScript, we can make our function accept an object.