Categories
JavaScript Answers

How to find the day, month and year with moment.js and JavaScript?

Sometimes, we want to find the day, month and year with moment.js and JavaScript.

In this article, we’ll look at how to find the day, month and year with moment.js and JavaScript.

How to find the day, month and year with moment.js and JavaScript?

To find the day, month and year with moment.js and JavaScript, we can use the date, month, and year methods.

For instance, we write

const date = moment().date();
const month = moment().month();
const year = moment().year();

to call moment to return a moment object with the current date and time.

Then we call date to return the day of the month.

We call month to return the month. month returns 0 for January and up to 11 for December.

And we call year to return the year.

Conclusion

To find the day, month and year with moment.js and JavaScript, we can use the date, month, and year methods.

Categories
JavaScript Answers

How to log an HTML element as a JavaScript object?

Sometimes, we want to log an HTML element as a JavaScript object.

In this article, we’ll look at how to log an HTML element as a JavaScript object.

How to log an HTML element as a JavaScript object?

To log an HTML element as a JavaScript object, we can use the console.dir method.

For instance, we write

const element = document.documentElement;
console.dir(element);

to get the document element with document.documentElement.

Then we assign that to element and log that with console.dir.

As a result, the properties of element will be displayed in the console.

Conclusion

To log an HTML element as a JavaScript object, we can use the console.dir method.

Categories
JavaScript Answers

How to remove style on an element with JavaScript?

Sometimes, we want to remove style on an element with JavaScript.

In this article, we’ll look at how to remove style on an element with JavaScript.

How to remove style on an element with JavaScript?

To remove style on an element with JavaScript, we can set the styles to null.

For instance, we write

const element = document.getElementById("sample_id");

element.style.width = null;
element.style.height = null;

to select the element with getElementById.

Then we set the width and height styles to null to remove the width and height styles of the element.

Conclusion

To remove style on an element with JavaScript, we can set the styles to null.

Categories
JavaScript Answers

How to fix dotenv file is not loading environment variables with JavaScript?

Sometimes, we want to fix dotenv file is not loading environment variables with JavaScript.

In this article, we’ll look at how to fix dotenv file is not loading environment variables with JavaScript.

How to fix dotenv file is not loading environment variables with JavaScript?

To fix dotenv file is not loading environment variables with JavaScript, we can call config to set the path to the .env file.

For instance, we write

const path = require("path");
require("dotenv").config({ path: path.resolve(__dirname, "../.env") });

to call config with an object with the path property that has the full path to the .env file that we want to load.

We call path.resolve with __dirname, which has the path of the project directory and the relative path to the .env file appended to it.

Conclusion

To fix dotenv file is not loading environment variables with JavaScript, we can call config to set the path to the .env file.

Categories
React Answers

How to set port in Next.js?

Sometimes, we want to set port in Next.js.

In this article, we’ll look at how to set port in Next.js.

How to set port in Next.js?

To set port in Next.js, we can use the -p option.

For instance, in package.json, we write

{
  //...
  "scripts": {
    "dev": "next -p 8080"
  }
  //...
}

to add -p 8080 to the dev script command to run the Next.js project on port 8080.

Conclusion

To set port in Next.js, we can use the -p option.