Categories
JavaScript Answers

How to get value at a specific index of array in JavaScript?

Sometimes, we want to get value at a specific index of array in JavaScript.

In this article, we’ll look at how to get value at a specific index of array in JavaScript.

How to get value at a specific index of array in JavaScript?

To get value at a specific index of array in JavaScript, we can use square brackets or the at method.

For instance, we write

const valueAtIndex1 = myValues[1];

to get the value at index 1 from the myValues array with square brackets.

We can do the same thing by calling at by writing

const valueAtIndex1 = myValues.at(1);

We can call at with negative indexes.

Negative indexes starts with -1 for the last item in an array.

Conclusion

To get value at a specific index of array in JavaScript, we can use square brackets or the at method.

Categories
JavaScript Answers

How to set custom HTML5 required field validation message with JavaScript?

Sometimes, we want to set custom HTML5 required field validation message with JavaScript.

In this article, we’ll look at how to set custom HTML5 required field validation message with JavaScript.

How to set custom HTML5 required field validation message with JavaScript?

To set custom HTML5 required field validation message with JavaScript, we can call the setCustomValidity method.

For instance, we write

<input type="text" name="email" id="emailElement" required />

to add an input element.

Then we write

const emailElement = document.getElementById("emailElement");

emailElement.addEventListener(
  "invalid",
  (e) => {
    const message = `${e.target.value}is not a valid email address`;
    emailElement.setCustomValidity(message);
  },
  false
);

to select the input with getElementById.

And then we call emailElement.addEventListener to listen for the invalid event.

In the event callback, we call emailElement.setCustomValidity with the validation error message.

Conclusion

To set custom HTML5 required field validation message with JavaScript, we can call the setCustomValidity method.

Categories
JavaScript Answers

How to change the current URL with JavaScript?

Sometimes, we want to change the current URL with JavaScript.

In this article, we’ll look at how to change the current URL with JavaScript.

How to change the current URL with JavaScript?

To change the current URL with JavaScript, we set the document.location.href property to the new URL.

For instance, we write

document.location.href = newUrl;

to set document.location.href to newUrl to navigate to the new URL.

Conclusion

To change the current URL with JavaScript, we set the document.location.href property to the new URL.

Categories
JavaScript Answers

How to write loops for promise with JavaScript?

Sometimes, we want to write loops for promise with JavaScript.

In this article, we’ll look at how to write loops for promise with JavaScript.

How to write loops for promise with JavaScript?

To write loops for promise with JavaScript, we can use the for-of loop with async and await.

For instance, we write

const sayHi = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log("Hi");
      resolve();
    }, 3000);
  });
};

const asyncArray = [sayHi, sayHi, sayHi];

const hi = async () => {
  for (const func of asyncArray) {
    console.log(await func());
  }
};

to define the sayHi function that returns a promise.

Then we define the asyncArray array with references to sayHi.

Next, we have the hi function that loops through the functions in asyncArray and call them in the for-of loop.

We use await to get the resolve value of each promise.

Conclusion

To write loops for promise with JavaScript, we can use the for-of loop with async and await.

Categories
React Answers

How to fix ‘Redirect’ is not exported from ‘react-router-dom’ with React Router v6?

Sometimes, we want to fix ‘Redirect’ is not exported from ‘react-router-dom’ with React Router v6.

In this article, we’ll look at how to fix ‘Redirect’ is not exported from ‘react-router-dom’ with React Router v6.

How to fix ‘Redirect’ is not exported from ‘react-router-dom’ with React Router v6?

To fix ‘Redirect’ is not exported from ‘react-router-dom’ with React Router v6, we should import Navigate instead of Redirect.

For instance, we write

import {
  BrowserRouter as Router,
  Routes,
  Route,
  Navigate,
} from "react-router-dom";
import Home from "../home/Home";

export default function App() {
  return (
    <Router>
      <Routes>
        <Route path="/home" element={<Home />} />
        <Route path="/" element={<Navigate replace to="/home" />} />
      </Routes>
    </Router>
  );
}

to use the Navigate component to redirect to the /home route when we go to / as specified by

<Route path="/" element={<Navigate replace to="/home" />} />

Conclusion

To fix ‘Redirect’ is not exported from ‘react-router-dom’ with React Router v6, we should import Navigate instead of Redirect.